Advertisement
Guest User

Waypoint: Caesars Cipher

a guest
Jan 1st, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rot13(encodedStr) {
  2.   var codeArr = encodedStr.split("");  // String to Array
  3.   var decodedArr = []; // Your Result goes here
  4.   // Only change code below this line
  5.   var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  6.  
  7.   for (var c = 0; c < codeArr.length; c++) //Count the number of words in codeArr
  8.     {
  9.       var newWord = "";
  10.       var newLetter = "";
  11.      
  12.       for (var i = 0; i < codeArr[c].length; i++) //Count the number of letters in codeArr[currentWord]
  13.         {
  14.           var letterCode = codeArr[c][i].charCodeAt(0); //Convert to ASCII Code
  15.          
  16.           if ((letterCode >= 65) && (letterCode <= 90)) //If the letter is A-Z
  17.             {
  18.              
  19.               for (var a = 0; codeArr[c][i] !== alphabet[a]; a++) //Go through alphabet checking for current letter
  20.                 {
  21.                   var newIndex = Math.abs(13 - a);
  22.                   newLetter = alphabet[alphabet.length - newIndex];
  23.                 }
  24.              
  25.             }
  26.          
  27.           else
  28.             {
  29.               newLetter = codeArr[c][i];
  30.             }
  31.           newWord += newLetter; //Add the newLetter to the newWord
  32.           newLetter = ""; //Clear the newLetter variable
  33.  
  34.         }
  35.       decodedArr.push(newWord); //Add the newWord to the decodedArr
  36.     }
  37.  
  38.  
  39.   // Only change code above this line
  40.   return decodedArr.join(""); // Array to String
  41. }
  42.  
  43. // Change the inputs below to test
  44. rot13("SERR PBQR PNZC");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement