ogora
May 18, 2017, 12:22pm
#1
Can anyone show me how to use a switch statement to solve Caesers cipher challenged. I have tried ths code below but it does not work. Can anyone help
rot13(str) {
var code = "";
switch {switch (str) {
case 'A':
answer = "N";
break;
case 'B:
answer = 'O';
break;
case 'C':
answer = "P";
break;
case 'D:
answer = 'Q"';
break;
case 'E:
answer = "R";
break;
case 'F:
answer = 'S';
break;
case 'G':
answer = "T";
break;
case 'H:
answer = "U";
break;
case 'I':
answer = "V";
break;
case 'J:
answer = 'W';
break;
case 'K':
answer = "X";
break;
case 'L:
answer = 'Y"';
break;
case 'M:
answer = 'Z"';
}
return str;
}
I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details .
1 Like
You have missing single quotes in various places in your code (like in 'B
, 'D
, etc).
You’ll also have to use a loop to iterate over each letters in the string.
Your code could look like
function rot13(str) {
var code = '';
for (...) {
switch (letter) {
case 'A':
// add 'N' to `code`
break;
case 'B':
// add 'O' to `code`
break;
...
}
}
return code;
}
1 Like
ogora
May 18, 2017, 2:20pm
#5
Part of your text is blurred. I cannot see it
It’s blurred because it’s a spoiler (contains a solution). Just click on it to remove the blur.
ogora
May 18, 2017, 2:33pm
#7
Ah… ok I will not touch it until I figure it out.
You can use for loop to solve this cipher.
If you still can get it checkout this https://github.com/Deityhub/Caesars-Cipher/blob/master/Cipher.js for a rundown of how thr code works.