Caesars Cipher - antoniojvargas/FreeCodeCamp GitHub Wiki
Caesars Cipher
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
function rot13(str) { // LBH QVQ VG!
var myStr ="";
var base = 65;
var top = 90;
var num = 0;
for (var i=0; i < str.length; i++){
num = str.charCodeAt(i);
if(num == 32){
myStr = myStr + " ";
} else {
if(num == 33 || num == 63 || num == 46){
num = num;
} else {
if(num - 13 < base){
num = (top - (base - (num - 13))) + 1;
} else {
num = num -13;
}
}
myStr = myStr + String.fromCharCode(num);
}
}
return myStr;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
A - 65
Z - 90