Solution to Caeser Cipher challenge

Continuing the discussion from freeCodeCamp Challenge Guide: Caesars Cipher:
With this solution, it’s easy to change the cipher shift.

  var newStr = "";
  for(var i = 0; i < str.length; i++) {
    newStr += shiftLetter(str[i], 13);
  }
  return newStr;
}
function shiftLetter(l, s) {
  l = l.toUpperCase();
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var newInd = (alphabet.indexOf(l)+s)%26;
  return alphabet.includes(l) ? alphabet[newInd] : l; 
}
console.log(rot13("SERR PBQR PNZC"));
5 Likes

Do you have a question about this challenge? We prefer that people don’t post solutions just for the sake of positing their solution.


We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

I’m sorry. I noticed a lot of other people posted their solutions on the challenge guide page, so that’s what I was trying to do.

It’s no problem. We’ve just moved away from massive threads of solutions as we’ve gained many more users and we’ve discovered that those threads don’t really foster discussion about how the solutions work.

1 Like
function rot13(str) {
  var cracker = {
    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", " ":" ", 
    ".":".", "!":"!", "?":"?"
  }

  return str.split("").map(char => cracker[char]).join("");
}

rot13("SERR PBQR PNZC!");

And here is my dumbest way to pass the test lol

4 Likes

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering other posters’s questions and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

3 Likes