Hi, I am struggling to work out why this code is failing the tests for Caeser’s Cipher, as it seems to produce the correct decoded answer:
var charCode = "";
var decodedArray = [];
function rot13(str) {
// loop through the provided string
for (i = 0; i < str.length; i++) {
// check if each character is a letter
if (str[i].match(/[a-z]/i)) {
//convert the letter to its character code and subtract 13 to decode it
charCode = str.charCodeAt(i) - 13;
// if the decoded character is lower than A (65) make the subtraction loop back to Z (90)
if (charCode < 65) {
charCode = charCode + 26;
}
// convert the character code back into a string, and push the decoded letter to 'decodedArray'
decodedArray.push(String.fromCharCode(charCode));
}
// in the loop, if a character is found which is not a letter, push it directly to 'decodedArray'
else {
decodedArray.push(str[i]);
}
}
// once the loop has completed, join decodedArray back into a string
str = decodedArray.join("");
return str;
}
// Change the inputs below to test
rot13("LBH QVQ VG!");
Does anyone know what I might be doing wrong?