I just completed the Ceasar’s Cipher Project of the JavaScript curriculum. Please comment and provide suggestions on how I could improve/refactor my code.
function rot13(str) {
let alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase();
let rot13Index = [];
let decodedStr = [];
for (let i = 0; i < str.length; i++) {
if (alphabet.indexOf(str[i]) !== -1) {
rot13Index.push(alphabet.indexOf(str[i]) + 13);
} else {
rot13Index.push(str[i]);
}
}
for (let j = 0; j < rot13Index.length; j++) {
if (typeof rot13Index[j] === "number" && rot13Index[j] < 26) {
decodedStr.push(alphabet[rot13Index[j]])
} else if (typeof rot13Index[j] === "number" && rot13Index[j] >= 26) {
decodedStr.push(alphabet[rot13Index[j] - 26]);
} else {
decodedStr.push(rot13Index[j]);
}
}
return decodedStr.join("");
}
console.log(rot13("SERR PBQR PNZC"));