Hello, I am working through the caesars cipher project but can’t figure out how to make my code function beyond this point.
I have looped through the input and an array containing alphabetical characters and created a new array named numbers, this has provided an array of numbers by alphabetical order that the input string contains. I have then looped through this array applying the ROT13 requirements (if the number is <= 13 then the index has added 13, if the index is > 13 then subtracted 13 to get a new indexed value.
Now I want to loop back through the alphabet array with the new index values and get the corresponding letter at each index to decipher the input string, however, at the moment I am getting a blank array:
Output:
[ 18, 4, 17, 17, 15, 1, 16, 17, 15, 13, 25, 2 ]
[ 5, 17, 4, 4, 2, 14, 3, 4, 2, 26, 12, 15 ]
[]
Line 1 is the output after the first loop, line 2 is the output after adding or subtracting 13, and the final line should be the corresponding letter at the index in line 2 of the alphabet array but it is coming up blank.
My code so far is:
function rot13(str) { // LBH QVQ VG!
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let alphArr = alphabet.split("");
let whitespace = /\s/g;
let newStr = str.split("");
let numbers = [];
let rotatedNum = [];
let result = [];
for(let i = 0; i < newStr.length; i++) {
for(let j = 0; j < alphArr.length; j++) {
if(newStr[i] === alphArr[j]) {
numbers.push(alphArr.indexOf(alphArr[j]));
}
}
}
for(let e = 0; e <= numbers.length; e++) {
if(numbers[e] <= 13) {
rotatedNum.push(numbers[e] + 13);
} else if(numbers[e] > 13) {
rotatedNum.push(numbers[e] - 13);
}
}
for(let x = 0; x <= alphArr; x++) {
for(let y = 0; y <= rotatedNum; y++) {
if(alphArr[x].indexOf(rotatedNum[y])) {
result.push(alphArr[x]);
}
}
}
console.log(numbers);
console.log(rotatedNum);
console.log(result);
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
Thank you