二元翻译项目

时间:2018-03-12 13:30:38

标签: javascript html

我正在开发一个类项目,允许人们将英语翻译成二进制文件,但似乎有一些错误,例如 空间错误,即使没有空间,也会添加空间的翻译。 多字母错误,每当我将2个相同的字母写在一起时,它只会记录第一个字母,除非写入另一个字母,否则没有其他字母。

(我的问题很独特,因为我有自己的代码,其操作方式与我被指责复制的方式不同,需要不同的解决方案。)

<!DOCTYPE html>
<html>

<head>
  <title>Binary Code Translator</title>
  <style>
    .textBoxes {
      width: 500px;
      height: 200px;
    }
    
    #translateButton {
      width: 100px;
      height: 100px;
    }
  </style>
</head>

<body>
  <h1>
    <center>Binary Code Translator</center>
  </h1>
  <hr>
  <br>
  <p>Input</p>
  <input type="text" name="inputBox" id="inputBox" class="textBoxes">
  <input type="button" name="translate" value="translate" id="translateButton" onclick="translateInput()">
  <p>Output</p>
  <input type="text" name="outputBox" id="outputBox" class="textBoxes">


  <script>
    function translateInput() {
      /*fetch input value*/
      var text = document.getElementById("inputBox").value;
      /*----A----*/
      if (text.includes("a")) {
        text = text.replace('a', '00110001 ');
      }
      if (text.includes("A")) {
        text = text.replace('A', '01000001 ');
      }
      /*----B----*/
      if (text.includes("b")) {
        text = text.replace('b', '01100010 ');
      }
      if (text.includes("B")) {
        text = text.replace('B', '01000010 ');
      }
      /*----c----*/
      if (text.includes("c")) {
        text = text.replace('c', '01100011 ');
      }
      if (text.includes("C")) {
        text = text.replace('C', '01000011 ');
      }
      /*----D----*/
      if (text.includes("d")) {
        text = text.replace('d', '01100011 ');
      }
      if (text.includes("D")) {
        text = text.replace('D', '01000011 ');
      }
      /*----E----*/
      if (text.includes("e")) {
        text = text.replace('e', '01100101 ');
      }
      if (text.includes("E")) {
        text = text.replace('E', '01000101 ');
      }
      /*----F----*/
      if (text.includes("f")) {
        text = text.replace('f', '01100110 ')
      }
      if (text.includes("F")) {
        text = text.replace('F', '01000110 ')
      }
      /*----G----*/
      if (text.includes("g")) {
        text = text.replace('g', '01100111 ')
      }
      if (text.includes("G")) {
        text = text.replace('G', '01000111 ')
      }
      /*----H----*/
      if (text.includes("h")) {
        text = text.replace('h', '01101000 ')
      }
      if (text.includes("H")) {
        text = text.replace('H', '01001000 ')
      }
      /*----I----*/
      if (text.includes("i")) {
        text = text.replace('i', '01101001 ')
      }
      if (text.includes("I")) {
        text = text.replace('I', '01001001 ')
      }
      /*----J----*/
      if (text.includes("j")) {
        text = text.replace('j', '01101010 ')
      }
      if (text.includes("J")) {
        text = text.replace('J', '01001010 ')
      }
      /*----K----*/
      if (text.includes("k")) {
        text = text.replace('k', '01101011 ')
      }
      if (text.includes("K")) {
        text = text.replace('K', '01001011 ')
      }
      /*----L----*/
      if (text.includes("l")) {
        text = text.replace('l', '01101010 ')
      }
      if (text.includes("L")) {
        text = text.replace('L', '01001010 ')
      }
      /*----M----*/
      if (text.includes("m")) {
        text = text.replace('m', '01101101 ')
      }
      if (text.includes("M")) {
        text = text.replace('M', '01001101 ')
      }
      /*----N----*/
      if (text.includes("n")) {
        text = text.replace('n', '01101110 ')
      }
      if (text.includes("N")) {
        text = text.replace('N', '01001110 ')
      }
      /*----O----*/
      if (text.includes("o")) {
        text = text.replace('o', '01101111 ')
      }
      if (text.includes("O")) {
        text = text.replace('O', '01001111 ')
      }
      /*----P----*/
      if (text.includes("p")) {
        text = text.replace('p', '01110000 ')
      }
      if (text.includes("P")) {
        text = text.replace('P', '01010000 ')
      }
      /*----Q----*/
      if (text.includes("q")) {
        text = text.replace('q', '01110001 ')
      }
      if (text.includes("Q")) {
        text = text.replace('Q', '01010001 ')
      }
      /*----R----*/
      if (text.includes("r")) {
        text = text.replace('r', '01110010 ')
      }
      if (text.includes("R")) {
        text = text.replace('R', '01010010 ')
      }
      /*----S----*/
      if (text.includes("s")) {
        text = text.replace('s', '01110011 ')
      }
      if (text.includes("S")) {
        text = text.replace('S', '01010011 ')
      }
      /*----T----*/
      if (text.includes("t")) {
        text = text.replace('t', '01110100 ')
      }
      if (text.includes("t")) {
        text = text.replace('t', '01010100 ')
      }
      /*----U----*/
      if (text.includes("u")) {
        text = text.replace('u', '01110101 ')
      }
      if (text.includes("U")) {
        text = text.replace('U', '01010101 ')
      }
      /*----V----*/
      if (text.includes("v")) {
        text = text.replace('v', '01110110 ')
      }
      if (text.includes("V")) {
        text = text.replace('V', '01010110 ')
      }
      /*----W----*/
      if (text.includes("w")) {
        text = text.replace('w', '01110111 ')
      }
      if (text.includes("W")) {
        text = text.replace('W', '01010111 ')
      }
      /*----X----*/
      if (text.includes("w")) {
        text = text.replace('w', '01111000 ')
      }
      if (text.includes("W")) {
        text = text.replace('W', '01011000 ')
      }
      /*----Y----*/
      if (text.includes("y")) {
        text = text.replace('y', '01111001 ')
      }
      if (text.includes("Y")) {
        text = text.replace('Y', '01011001 ')
      }
      /*----Z----*/
      if (text.includes("z")) {
        text = text.replace('z', '01111010 ')
      }
      if (text.includes("Z")) {
        text = text.replace('Z', '01011010 ')
      }
      /*----space----*/
      if (text.includes(" ")) {
        text = text.replace(' ', '00100000 ')
      }
      /*----dot----*/
      if (text.includes(".")) {
        text = text.replace('.', '00101110 ')
      }
      /*----,----*/
      if (text.includes(",")) {
        text = text.replace(',', '00100111 ')
      }
      /*----:----*/
      if (text.includes(":")) {
        text = text.replace(':', '00110001 ')
      }
      /*----;----*/
      if (text.includes(";")) {
        text = text.replace(';', '00111011 ')
      }
      /*----?----*/
      if (text.includes("?")) {
        text = text.replace('?', '00111111 ')
      }
      /*----!----*/
      if (text.includes("!")) {
        text = text.replace('!', '00100001 ')
      }
      /*----`----*/
      if (text.includes("`")) {
        text = text.replace('`', '00101100 ')
      }
      /*----"----*/
      if (text.includes("\"")) {
        text = text.replace('`', '00101100 ')
      }
      /*----(----*/
      if (text.includes("(")) {
        text = text.replace('(', '00101000 ')
      }
      /*----)----*/
      if (text.includes(")")) {
        text = text.replace(')', '00101001 ')
      }
      /*make output value equal to input value*/
      document.getElementById("outputBox").value = text;
    }
  </script>

</body>

</html>

1 个答案:

答案 0 :(得分:-1)

使用 .map 的功能, 为您的代码示例:

    

<head>
  <title>Binary Code Translator</title>
  <style>
    .textBoxes {
      width: 500px;
      height: 200px;
    }

    #translateButton {
      width: 100px;
      height: 100px;
    }
  </style>
</head>

<body>
  <h1>
    <center>Binary Code Translator</center>
  </h1>
  <hr>
  <br>
  <p>Input</p>
  <input type="text" name="inputBox" id="inputBox" class="textBoxes">
  <input type="button" name="translate" value="translate" id="translateButton" onclick="translateInput()">
  <p>Output</p>
  <input type="text" name="outputBox" id="outputBox" class="textBoxes">


  <script>
function textToBinary(string) {
 return string.split('').map(function (char) {
        return char.charCodeAt(0).toString(2);
    }).join('');
}


    function translateInput() {
      /*fetch input value*/
      var text = document.getElementById("inputBox").value;
      var outputtext=textToBinary(text);
      document.getElementById("outputBox").value = outputtext;
    }
  </script>

</body>

</html>