Hello. This is my first time creating a post. I am currently working on the caesar cipher problem from freecodecamp javascript section. I am having hard time figuring out what the error is in my code or how to explain it. I know where its messing up, However I do not know why. I will post the code. I can see that any letter before O does not switch however any letter after it does switch to the correct letter. Thank you very much.
function rot13(str) {
const letters = { // cipher translation
A: 'N',
B: 'O',
C: 'P',
D: 'Q',
E: 'R',
F: 'S',
G: 'T',
H: 'U',
I: 'V',
J: 'W',
K: 'X',
L: 'Y',
M: 'Z',
N: 'A',
O: 'B',
P: 'C',
Q: 'D',
R: 'E',
S: 'F',
T: 'G',
U: 'H',
V: 'I',
W: 'J',
X: 'K',
Y: 'L',
Z: 'M'
}
// looping through keys searching for the key within the string. then replacing with the value of that letters[key]
for(let key in letters) {
let regex = new RegExp(key, 'g');
str = str.replace(regex, letters[key]);
}
return str;
}