Tell us what’s happening:
Hi, My second for loop is reversing the changes made in my first loop. Why is that?
Your code so far
function rot13(str) {
let rot1 = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' ];
let rot2 = [ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
let newArr = str.split("")
let newStr = newArr;
// If number letter in str is equal to a letter in rot1 array than change that letter to a letter in rot2 with the same index as rot1.
for (let i = 0; i < newStr.length; i++){
for (let j = 0; j < rot1.length; j++){
if(rot1[j] === newStr[i]){
newStr[i] = rot2[j]
}
}
}
// If number letter in str is equal to a letter in rot2 array than change that letter to a letter in rot1 with the same index as rot2.
for (let i = 0; i < newStr.length; i++){
for (let k = 0; k < rot2.length; k++){
if(rot2[k] === newStr[i]){
newStr[i] = rot1[k]
}
}
}
return newStr
}
let result = rot13("SERR CVMMN!");
console.log(result)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 OPR/90.0.4480.84
Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher
Link to the challenge: