Caesars Cipher Solution

I didn’t find a link to post Caesars Cipher solution so I made my own. The cool thing is that I though of this one before following the video, and usually I’ll get stuck trying to think of solutions. Then I lost the code so had to try to recreate it by memory! Hope it makes sense.

function rot13(str) {
  let decodedStr = "";
  for(let i = 0; i < str.length; i++) {
    const char = str[i];
    if(/[A-Z]/.test(char)) {
      let code = str.charCodeAt(i);
      if(char > 'M') {
        code -= 13;
      } else {
        code += 13;
      }
      decodedStr += String.fromCharCode(code);
    } else {
      decodedStr += char;
    }
  }
  return decodedStr;
}
console.log(rot13("SERR PBQR PNZC"));
3 Likes

Nice job! I’m amazed that you can compare strings like that :slight_smile:

By the way, the thread is here

1 Like

One of the simplest and therefore efficient solutions I have seen.

Wow amazing, I love this and motivated to put in more effort to get to where you are. Thanks for sharing