I'm making Cesar's Cypher that shifts the letters 13 places. It works, but the last letter of each word is repeated. I know there is probably a more efficient way to write this, but here is my code.
function rot13(str) {
var letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var word = "";
var z = 30;
for (i = 0; i < str.length; i++) {
if (str.substring(i, i + 1) === " ") {
word = word + str.substring(i, i + 1);
}
for (x = 0; x < letters.length; x++) {
if (str.substring(i, i + 1) === letters[x]) {
z = x; //18
}
}
var n = z + 13; //31
if (n >= 26) {
word = word + letters[13 - (26 - z)];
} else if (n < 26)
word = word + letters[13 + z];
}
return word;
}
rot13("LBH QVQ VG!");
For example LBH QVQ VG! prints out YOU UDID DITT instead of YOU DID IT!. Also, I'm new to javascript so if any syntax looks off please correct me.