Journal Archive

function rot13(message) {
  function shiftBy13(letter) {
    const charCode = letter.charCodeAt(0);
    if ( charCode >= 65 && charCode <=90) {
      return charCode + 13 > 90 ? 
        String.fromCharCode(charCode + 13 - 26) : 
        String.fromCharCode(charCode + 13);
    } else if (charCode >= 97 && charCode <= 122) {
      return charCode + 13 > 122 ? 
        String.fromCharCode(charCode + 13 - 26) : 
            String.fromCharCode(charCode + 13);
    }
  }
  return message.replace(/[A-Za-z]/g, shiftBy13);
  

Day 10: Solving one of the Kata on CodeWars

Rot13 5 kyu

ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher.

Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation".