Caesars Cipher Using Switch Statements

Tell us what’s happening:
My code works for the letters but I forgot about punctuation. A switch statement may not have been the best choice but I wanted the practice. So now I am debating the use of .indexOf() and/or .replace() to tweak it. I could also just hard wire non non-letters into the case list but that seems like cheating. Is there a more elegant way to achieve the result?

Your code so far


function rot13(str) {
var arr = [];

for (var i = 0; i < str.length; i++)

switch(str[i]) {
case "!":
arr.push("!");
break;  
case " ":
arr.push(" ");
break;
case "A":
arr.push("N");
 break;
case "B":
arr.push("O");
 break;
case "C":
arr.push("P");
 break;
case "D":
arr.push("Q");
 break;
case "E":
arr.push("R");
 break;
case "F":
arr.push("S");
 break;
case "G":
arr.push("T");
 break;
case "H":
arr.push("U");
 break;
case "I":
arr.push("V");
 break;
case "J":
arr.push("W");
 break;
case "K":
arr.push("X");
 break;
case "L":
arr.push("Y");
 break;
case "M":
arr.push("Z");
 break;
case "N":
arr.push("A");
 break;
case "O":
arr.push("B");
 break;
case "P":
arr.push("C");
 break;
case "Q":
arr.push("D");
 break;
case "R":
arr.push("E");
 break;
case "S":
arr.push("F");
 break;
case "T":
arr.push("G");
 break;
case "U":
arr.push("H");
 break;
case "V":
arr.push("I");
 break;
case "W":
arr.push("J");
 break;
case "X":
arr.push("K");
 break;
case "Y":
arr.push("L");
 break;
case "Z":
arr.push("M");

}
var joined = arr.join("");
console.log(joined);
}

rot13("SERR CVMMN!");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Caesars Cipher

Link to the challenge:

Hello~!

As a hint, a switch statement can include a default at the end, to run if no case conditions are met. :slight_smile:

just now noticed your reply. I just stitched it up after this post and haven’t revisited it…I’m probably use this in my gallery eventually so that’s a big help…

case x:
    // code block
    break;
  default:
    arr.push(str[i]);
}

like this?

Did you test it? It should work but your function should return the joined string not just log it.

Considering how hardcoded your solution already is I don’t really see how accounting for the symbols would change that fact all that much.

Programs are meant to do the work for you, not the other way.

The more you hardcode the output the less useful the program is, it also becomes inflexible and hard to change. What if you were asked to shift the letters by some different amount? Would you then change all the letters again by hand? What if all you needed to change was a number? The number by which you are shifting the letters. Now to meet the new requirement all you would have to do is change the number.

Here’s what I used to pass a few minutes after I wrote the OP…

function rot13(str) {
var arr = [];

for (var i = 0; i < str.length; i++)

switch(str[i]) {
case ",":
arr.push(",");
   break; 
case ".":
arr.push(".");
   break; 
case "?":
arr.push("?");
   break; 
case "!":
arr.push("!");
   break;  
case " ":
arr.push(" ");
   break;
case "A":
arr.push("N");
    break;
case "B":
arr.push("O");
    break;
case "C":
arr.push("P");
    break;
case "D":
arr.push("Q");
    break;
case "E":
arr.push("R");
    break;
case "F":
arr.push("S");
    break;
case "G":
arr.push("T");
    break;
case "H":
arr.push("U");
    break;
case "I":
arr.push("V");
    break;
case "J":
arr.push("W");
    break;
case "K":
arr.push("X");
    break;
case "L":
arr.push("Y");
    break;
case "M":
arr.push("Z");
    break;
case "N":
arr.push("A");
    break;
case "O":
arr.push("B");
    break;
case "P":
arr.push("C");
    break;
case "Q":
arr.push("D");
    break;
case "R":
arr.push("E");
    break;
case "S":
arr.push("F");
    break;
case "T":
arr.push("G");
    break;
case "U":
arr.push("H");
    break;
case "V":
arr.push("I");
    break;
case "W":
arr.push("J");
    break;
case "X":
arr.push("K");
    break;
case "Y":
arr.push("L");
    break;
case "Z":
arr.push("M");
   
}
var joined = arr.join("");
return joined;
}

rot13("SERR CVMMN!");

Haven’t tested the default code yet, but I will swing back around after I figure out the phone number and cash register problems. Also, want to see if I can put functions in the for a case. So what you’re saying sounds like we want the variables to be versatile, powerful and clearly delineated from each other. That’s a great point I will have to take to heart. At this point I am just lucky if I can bring my own ideas out instead of relying on solutions. Unfortunately sometimes that means having clunky code.

1 Like

…tried it out and it works perfectly!!

1 Like

Good job.

Seeing as you have passed this challenge with your current code, I would now suggest you try solving it again using a more programmatic approach.

If you look at the hint page (you do not have to look at the solutions) you will see two methods mentioned. Look at MDN for the string methods and see if you can figure out how they might be useful for solving this challenge.

I was thinking that. I had two other solutions on the drawing board at the time. Those ones just had an array where you [i +13]

1 Like