JavaScript Algorithms and Data Structures Projects - Caesars Cipher

My code is not working, i want to replace the letters in the string with the letters that are 13 after them in alphabetical order. Or the ones which are before them by 13.
(Letters after M should be replaced by the letters that are 13 before them. )

I wrote the code below, you can see my solution, yet it still not working, when i use return i get the same result as before writing the code.

const rot13 = string => {
for (let B = 0; B < string.length; B = B + 1) {
  let findCharCode = string[B].charCodeAt();
if (65 <= findCharCode < 78) {
findCharCode += 13;
  string.replace(string[B], String.fromCharCode(findCharCode));
} else if (77 < findCharCode < 91) {
  findCharCode -= 13;
  string.replace(string[B], String.fromCharCode(findCharCode));
} else if (96 < findCharCode < 110) {
  findCharCode += 13;
  string.replace(string[B], String.fromCharCode(findCharCode));
} else if (109 < findCharCode < 123) {
  findCharCode -= 13;
  string.replace(string[B], String.fromCharCode(findCharCode));
} else {
  string;
}
};
return string;
};

console.log(rot13('az AZ')); // nm NM
console.log(rot13('10+2 is twelve.')); // 10+2 vf gjryir.
console.log(rot13('abcdefghijklmnopqrstuvwxyz')); // nopqrstuvwxyzabcdefghijklm


Begin troublshooting by using console.log() to print out variables at different points in your code.

I’ve added some here. You can see that when findCharCode is 97, the correct if else never executes.

The first if always executes and the string never changes. If you add console.log(string) after every string.replace you can see it’s always the same.

const rot13 = string => {
    for (let B = 0; B < string.length; B = B + 1) {
        let findCharCode = string[B].charCodeAt();
        
        console.log(findCharCode);
        
        if (65 <= findCharCode < 78) {
        findCharCode += 13;
          string.replace(string[B], String.fromCharCode(findCharCode));
        } else if (77 < findCharCode < 91) {
          findCharCode -= 13;
          string.replace(string[B], String.fromCharCode(findCharCode));
        } else if (96 < findCharCode < 110) {
          
          console.log("findCharCode 97");
          
          findCharCode += 13;
          string.replace(string[B], String.fromCharCode(findCharCode));
        } else if (109 < findCharCode < 123) {
          findCharCode -= 13;
          string.replace(string[B], String.fromCharCode(findCharCode));
        } else {
          console.log("else executes")
          string;
        }
};
return string;
};

console.log(rot13('az AZ')); // nm NM
console.log(rot13('10+2 is twelve.')); // 10+2 vf gjryir.
console.log(rot13('abcdefghijklmnopqrstuvwxyz')); // nopqrstuvwxyzabcdefghijklm

I don’t get your point

Add this console.log line to your code and run it

Here is the output:

65 <= findCharCode < 78
97
65 <= findCharCode < 78
122
65 <= findCharCode < 78
32
65 <= findCharCode < 78
65
65 <= findCharCode < 78
90
az AZ
1 Like

I already know that it’s never changing, but i son’t know why

This does not appear to be working as you intend. I would try coding this in a different way.