Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

javascript - Program to write and decode coded messages glitches out when textarea is substituted for input type = text

I have a program that generates and decodes secret messages using an encryption sequence and the message, the letters are converted to numbers, the numbers/letters are added with their corresponding numbers in the randomly generated encryption sequence, and the program decodes it by subtracting, but I substituted for to account for longer messages, it glitched out. See for yourself. I have isolated the problem to the textArea. Here's my code.

<!DOCTYPE html>
<html>
  <h1>DECODE</h1>
  <p>Message here</p>
  <textarea id="coded message" rows="5" cols="50"> </textarea>
  <p>Encryption sequence here</p>
  <textarea id="encryption sequence" rows="5" cols="50"> </textarea>
  <button onclick="toArray()">DECODE</button>
  <p id="output"></p>
</html>
<script>
  var seq = document.getElementById("encryption sequence").value;
  var mes = document.getElementById("coded message").value;
  var convertedSeqArray = [];
  var messageDoneString = "";
  var convertedMesArray = [];
  var subArray = [];
  var mesArray = [];
  var x = 0;
  var convertedMesArray = [];
  var convertedSeqArray = [];
  var messageDone = [];
  seqArray = [];
  var num = "";
  function toArray() {
    mes = document.getElementById("coded message").value;
    mesArray = mes.split(" ");
    seq = document.getElementById("encryption sequence").value;
    seqArray = seq.split(",");
    convertedSeqArray = seqArray.map(Number);
    convertedMesArray = mesArray.map(Number);
    for (var l = 0; l < mesArray.length; l++) {
      subArray.push(convertedMesArray[l] - convertedSeqArray[l]);

      num = String.fromCharCode(96 + subArray[l]);

      messageDone.push(num);
    }
    for (var q = 0; q < messageDone.length; q++) {
      messageDoneString = messageDoneString + messageDone[q];
    }
    alert(messageDoneString);
  }
</script>
<html>
  <h1>ENCODE</h1>
  <p>Encryption sequence:</p>
  <p id="sequence"></p>
  <p>Coded message</p>
  <p id="coded"></p>
  <textarea id="h" rows = '5' cols = '50'> </textarea>

  <button onclick="getList()">ENCODE</button>
  <script>
    var seq = document.getElementById("sequence");
    var cod = document.getElementById("coded");
    var array = [];
    var output = document.getElementById("h").value.split("");
    var sequence = [];
    var sequenceCounter = 0;
    function generateSequence(length) {
      sequence = [];
      for (var c = 0; c < length; c++) {
        sequence.push(Math.floor(Math.random() * 10));
      }
    }
    var message = "";
    function getList() {
      output = document.getElementById("h").value.split("");
      array = output;
      var length = array.length;
      generateSequence(array.length);
      message = "";
      for (var i = 0; i < array.length; i++) {
        var addNew = array[i].charCodeAt(0) - 96;

        addNew = addNew + sequence[i];
        message = message + " " + addNew;
      }

      cod.innerText = message;
      seq.innerText = sequence;
    }
  </script>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is the spaces in the beginning. If there are any spaces in the beginning, you're gonna get those weird letters. I tried fixing it with trim(), which didn't work. For some reason, when you copy and paste the "Coded message," it pastes with a space in the beginning. You could use an if statement to remove the space.

    mes = document.getElementById("coded message").value.trim();
    mesArray = mes.split(" ");
    seq = document.getElementById("encryption sequence").value.trim();
    seqArray = seq.split(",");
    if (mes[0] === " ")
      mes.shift();
    if (seq[0] === " ")
      seq.shift();
    convertedSeqArray = seqArray.map(Number);
    convertedMesArray = mesArray.map(Number);
    for (var l = 0; l < mesArray.length; l++) {
      subArray.push(convertedMesArray[l] - convertedSeqArray[l]);

      num = String.fromCharCode(96 + subArray[l]);

      messageDone.push(num);
    }
    for (var q = 0; q < messageDone.length; q++) {
      messageDoneString = messageDoneString + messageDone[q];
    }

I did use trim since it does remove enough spaces until there is one left and also any extra spaces (which shouldn't occur). Since both strings become arrays, we can use array functions on the variables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...