kickinbahk
1/19/2016 - 3:53 AM

Solution For FreeCodeCamp's Caesars Cipher

Solution For FreeCodeCamp's Caesars Cipher

function rot13(str) { // LBH QVQ VG!  
  
  function convertToArray(string) {
    var convertedArr = [];
    var num = 0;
    for(i = 0; i < string.length; i+=1) {
      num = string.charCodeAt(i);
      convertedArr.push(num);
    } 
    return convertedArr;
  }
  
  function convertToUniCode (charString) {
    var newArr = [];
    for(x = 0; x < charString.length; x+=1) {
      if (charString[x] < 65 || charString[x] > 91) {
        newArr.push(charString[x]);
      } else if (charString[x] > 77) {
        newArr.push(charString[x] - 13);
      } else {
        newArr.push(charString[x] + 13);
      }
    }
    return newArr;
  }
  
  function convertToString (stringToTest) {
    return String.fromCharCode.apply(this, convertToUniCode(convertToArray(stringToTest)));
  }
  return convertToString(str);
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");