Caesar's Cipher

Tell us what’s happening:
I’m stuck, please help. I think it should at least work for the first test case “SERR PBQR PNZC” but it doesn’t…

   **Your code so far**

function rot13(str) {
let letterPairs = {
   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'    
let decipheredString = '';
var firstLevel = str.split(" ")

var newArr = []

for(let i in firstLevel){
  var x = [...firstLevel[i]]

 for(let i = 0 ; i < newArr.length; i++){
   for (let j = 0; j < newArr[i].length; j++){
     decipheredString += letterPairs[newArr[i][j]];
   decipheredString += " "



rot13("SERR PBQR PNZC!");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Caesars Cipher

Link to the challenge:

How are you handling punctuation?

Research charCodeAt():

'ABC'.charCodeAt(0)  // = 65

Then research fromCharCode:

String.fromCharCode(65, 66, 67);   // returns "ABC"

Use these two methods to create the ROT13.

1 Like

Sure, that is a way to do it, but OP’s dictionary lookup approach is also perfectly valid.

1 Like

To add to @JeremyLT how are you handling spaces, or numbers? You have a solid pattern for alphabetic cases, franklyi like the lookup object, as it’s fast and consistent. But have you considered how to handle characters that aren’t in your lookup object?

1 Like

I would say its not fast and consistent. In order to make this approach work, every single symbol and character code needs to be inputted into the table. This leaves room for mistakes and only allows for the characters in the table.

If anyone else wanted to do a rot 12 cipher with this program later in production, the table is now useless and a new one needs to be made. This mean that you need 25 object tables, each with thousands of key value pairs, in order to account for all the possibilities. To me this removes the principals of functional programming and using methods given in JS.

This is not true. You do not need to have everything in the lookup table. Only the 25 letters are needed.

This is true, but this is not a reason to tell the OP to throw away their code and start over. Learning how to fix the bug in the current version provides insight and a starting point for a future conversation about refactoring the fixed code.

The OP is very close to a working solution. They don’t need to go back to square one right now.

I’ve found it much better for overall understanding to help the learner work their code towards a good solution rather than telling them to throw away their hard work thus far and start over. I reserve that only for when the learners code cannot reach a functional state.

1 Like

I would say its best to solidify more solid coding practices early on. However, you are right that possibly finding a bug in their code is better then just starting over.

As long as the user knows that the built in functions are there for usage. Problems dealing with binary later on will need it the most.

Sure, best practices are great, but sometimes you need to play around with the problem and get something working before you can see a better approach.

I think one of the biggest practices for professional devs to internalize early on is “don’t let perfect be the enemy of done”. There is always time to refactor code later.

2 posts were split to a new topic: Is Modern Crypto a Shift Cypher

Very well done! Great detective work. One suggestion, though: when posting working code, please wrap it in spoiler tags to blur it. This way, others looking for help for similar problems aren’t simply seeing a complete solution.

Now, this is an excellent first step. You did really really well. But yes, your skills will evolve. As you learn new tools, you might see other ways of approaching past challenges, ways that might lead to more robust or extensible solutions. None of that invalidates what you’ve done, so keep a copy of this one (I’d put it in a repl or a github repo so you can tinker) and as you grow, add future revisions as steps in your evolution.

1 Like

My first pass at this was similar to the OP’s, though using a pair of strings rather than a lookup object…because it’s what i knew. I understood the challenge, but i lacked all the tools at that point.

I could have sat and waited until I’d learned more, but that’s not my way. I’ll try and build things with the limited toolset, knowing I’ll find different ways later.

And so yes, i revisited this with a lookup object, then again with a cipher factory, and again with a functional programming perspective. And frankly, i know i haven’t learned all there is to learn in this one. I’m wondering if a bitwise operation might be something to explore.

My point? The OP met the spec of this one, and passed the challenge. Could it be done differently? Sure. Some use strings, some use fromCharCode, some use whatever they understand. In this, FCC deliberately didn’t limit the student. You’re not wrong about standards being different in a production environment, but this isn’t that. This is an early step *toward *that, but the challenge is both about the logic and the code.


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