Caesars Cipher initial thoughts before tomorrow's class

Hi all, here is my working where I was trying to think out a way of approaching the Caesar Cipher before tomorrow’s class. I did not get anywhere, but wanted to record what I did here in case anyone has any thoughts. Also, if I have time, I will try and improve my code after viewing tomorrow’s video.

function rot13(str) {
switch (str) {
case “A”:
console.log(“N”);
break;
case “B”:
console.log(“O”);
break;
case “C”:
console.log(“P”);
break;
case “D”:
console.log(“Q”);
break;
case “E”:
console.log(“R”);
break;
case “F”:
console.log(“S”);
break;
case “G”:
console.log(“T”);
break;
case “H”:
console.log(“U”);
break;
case “I”:
console.log(“V”);
break;
case “J”:
console.log(“W”);
break;
case “K”:
console.log(“X”);
break;
case “L”:
console.log(“Y”);
break;
case “M”:
console.log(“Z”);
break;
case “M”:
console.log(“A”);
break;
case “N”:
console.log(“B”);
break;
case “O”:
console.log(“C”);
break;
case “P”:
console.log(“D”);
break;
case “Q”:
console.log(“E”);
break;
case “R”:
console.log(“F”);
break;
case “S”:
console.log(“G”);
break;
case “T”:
console.log(“H”);
break;
case “U”:
console.log(“I”);
break;
case “V”:
console.log(“J”);
break;
case “W”:
console.log(“K”);
break;
case “X”:
console.log(“L”);
break;
case “Y”:
console.log(“M”);
break;
case “Z”:
console.log(“N”);
break;
default:
console.log(“I am lost”);

}

}
// I used console.log for the switch cases as this was the only way i could get an output - answer = “N”, e.g., did nothing
//Why is there a separate “undefined” line under each console.log return???
// How do i do multiple letter conversions at the same time? Turn a string into an array then back into a string after conversion? If so, how do i do this?
console.log(rot13(“A”));
console.log(rot13(“B”));
console.log(rot13(“M”));
console.log(rot13(“SERR PBQR PNZC”));

contents of my console:

N
undefined
O
undefined
Z
undefined
I am lost
undefined

4 Likes

Hi Jane,

I have not tried this challenge yet, but it is so good to see that you are working through these problems and writing down your thought process. It is an excellent way of learning.

I noticed you post your code as ‘quote’, but if you post it as ‘code’ (wrap between tripple backticks, on most keyboards left of the 1-key) it keeps the formatting and code is easier to follow along. You should also be able to select all the code and use the </> icon.

Keep going!

3 Likes

Hello!
Let me explain what your code is doing right now:
console.log(rot13(“A”));
you call the function console.log, that calls the function rot13 with the argument “A”
→ in the switch statement in rot13, there is a case for “A”, so that case is being executed → console.log(“N”); prints an N to your console. After that the break ends the switch statement and the function continues, but there is nothing else in the function => rot13 finishes without a return value, so it returns a undefined => console.log(undefined);
Result:

N
undefined
3 Likes

This is the version I came up with - it’s actually my second version, I used a jS object as a cypher table and replaced a messy if then else statement into an arrow function.

function rot13(str) {
  const cypherMap = (cypherFix) => fixed = fixed.concat(cypherTable[cypherFix]);
 //the arrow function looks up S in cypherTable, retrieves the value "F" and contatenates it to fixed and the next iteration of the for loop commences													
  const cypherTable = {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 cypherFix = ("");
  let fixed = ("");
  for ( let i = 0; i < str.length; i++) {  //this traverses the string starting with str[0] to 1 less than the length of the string
    cypherFix = str[i]		         //str[0] = S
    cypherMap(cypherFix);	//this calls cypherMap with a value of S
  }
  return fixed;
}
rot13("SERR CVMMN!")
3 Likes

You could try modify cypherMap so it doesn’t need a complete table, then you don’t have a problem if there is a char that’s not in the table (i.e. %)
method:

  1. if cypherFix is in the cypherTable use cypherTable[cypherFix]
    2.if not, use directly cypherFix
3 Likes

Hi Tzerio,

Yep, I see - I have just been watching the video and realise that this should be done as you mention.

3 Likes

Thank you Tzerio, that is very helpful!

2 Likes

I have fixed the code by taking the hard coded punctuation out of the object but at the cost of putting in an if statement. I would really like to get this into the arrow function, but putting a logical or || in there doesn’t work as I thought it would. I expected it , upon not finding a non letter character in the cypherTable call, to move on to the second part of the arrow function, but instead it just returns undefined and puts undefined into the string fixed. So I wanted this line:

const cypherMap = (cypherFix) => fixed = fixed.concat(cypherTable[cypherFix]); 

to be this:

const cypherMap = ((cypherFix) => fixed = fixed.concat(cypherTable[cypherFix])||(cypherFix) => fixed = fixed.concat(cypherFix)); 

which I hoped would leave my for loop looking like this:

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

If anyone has an idea how this can be done I would be interested to hear it.

function rot13(str) {
  const cypherMap = (cypherFix) => fixed = fixed.concat(cypherTable[cypherFix]);                                        
  const cypherTable = {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 cypherFix = ("");
  let fixed = ("");
  for ( let i = 0; i < str.length; i++) {
    cypherFix = str[i];       
    if (/[A-Z]/.test(cypherFix)) {
      cypherMap(cypherFix);        
      }else{
    fixed = fixed.concat([cypherFix]);
    }
  }
  return fixed;
}
rot13("SERR CVMMN!")
2 Likes

I think || would be possible if the first option throws an error because of something not being declared
You can look into the ternary operator, that’s a way to replace an if-statement:
(condition) ? (return value if condition true) : (return value if condition false)

You can use that operator inline and you have to work with the return value, so just complete this:
const cypherMap = (cypherFix) => fixed = (condition ? if true : if false )

2 Likes

So good indeed. I am yet to start mine but hope to do so soonest. Well done!

2 Likes

Tzerio, thank you so much, that is brilliant! This, then is my final code and yes, it does work!

function rot13(str) {
  const cypherMap = (cypherFix) => fixed = ((/[A-Z]/.test(cypherFix)) ? fixed.concat(cypherTable[cypherFix]) : fixed.concat(cypherFix));                                        
  const cypherTable = {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 cypherFix = ("");
  let fixed = ("");
  for ( let i = 0; i < str.length; i++) {
    cypherFix = str[i];       
      cypherMap(cypherFix);
  }
  return fixed;
}
rot13("SERR CVMMN!")

I really feel that I learned a great deal doing this particular problem, but your help was very definitely the icing on the cake and it certainly taught me something I thought could be done but didn’t know how to go about it.

Cheers

Bill

3 Likes

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