Saturday, January 2, 2016

Caesar's Cipher Waypoint/Bonfire on Free Code Camp Solution

I'm working on front end development in Free Code Camp and haven't been able to walk away from my computer since I started except when I get stuck and hate coding and want to quit. Anyway, since the new waypoints/bonfires were added to the Basic JavaScript section yesterday, I hadn't been able to figure out Caesar's Cipher and was especially frustrated that I couldn't find any help on line and I'd become slightly dependent. So, since I finally figured it out and there is still no solution online, I figured I'd upload mine and hope it helps someone.

Even though you're probably just here to find some answers, I'm gonna explain why I did what I did.

1) First, I needed a way to transform letters into a letter 13 letters down the alphabet. I had a bunch of ideas about how to do this, but finally settled on using a huge switch statement. (For example, if the letter is A, make it N)
2) Since I eventually return the Free Code Camp-provided variable decodedArr, I figured the best way to store my answers from step 1 was in my decoded array
3) I needed a way to put every letter from encodedStr through my switch statement, so I made a for loop with codeArr[i].
That's it. It took me a million years to crack it but written out in three steps it looks stupidly easy.

*SOLUTION*


Here's a link to my solution on Free Code Camp if you want to see it with pretty colors and here is my FCC profile


There's probably a fancy way to format this on here so it doesn't look gross but I'm sorry:

function rot13(encodedStr) {

  var codeArr = encodedStr.split("");  // String to Array
  var decodedArr = []; // Your Result goes here
  // Only change code below this line

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

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

  // Only change code above this line

  return decodedArr.join(""); // Array to String
}

// Change the inputs below to test

rot13("SERR PBQR PNZC");



I can't promise that's a really efficient way to code it, but I can tell you that it made all the checkmarks green. Good luck!