Post your Caesar's Cipher solutions here! (Fall 2022)

decided to do mine before watching the class, i looked at converting the alphabet to integer value and then looping with a +13 and back to a character but decided on hard coding in it an array was faster and less complicated.

non-alphabetic characters (i.e. spaces, punctuation) etc didn’t pass through so i added them to the array.

function rot13(str) {
  const caesar = {
    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',
    " ": " ",
    "!": "!",
    "?": "?",
    ".": "."
  };
  var code = "";
  for (let i = 0; i < str.length; i++) {
    code += caesar[str[i]];
  }
   return code;
}

rot13("SERR PBQR PNZC");

console.log(rot13("SERR CVMMN!"));
1 Like

Here is my code for the caesar’s cipher:

function rot13(str) {
    let decodedString = "";
    let alphabetStart = "ABCDEFGHIJKLM";
    let alphabetEnd = "NOPQRSTUVWXYZ";

    for (let i = 0; i < str.length; i++) {
      let letterToDecode = str[i];

        if (alphabetStart.indexOf(letterToDecode) >= 0) {
          decodedString += alphabetEnd[alphabetStart.indexOf(letterToDecode)];
        }
        else if (alphabetEnd.indexOf(letterToDecode) >= 0) {
          decodedString += alphabetStart[alphabetEnd.indexOf(letterToDecode)];
        } else {
          decodedString += letterToDecode;
        }
    }
    return decodedString;
}

let result = rot13 ("SERR PBQR PNZC");
console.log(result);
1 Like

Well done!!

Keep going, just two more steps… :smile: :mechanical_arm:

Happy coding!

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.