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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…