Skip to content

Instantly share code, notes, and snippets.

@AlexandroPerez
Created November 24, 2016 01:05
Show Gist options
  • Save AlexandroPerez/49e1bb227986407d6bac104f0650a71f to your computer and use it in GitHub Desktop.
Save AlexandroPerez/49e1bb227986407d6bac104f0650a71f to your computer and use it in GitHub Desktop.
Free Code Camp - Basic Algorithm Scripting - Caesars Cipher
function rot13(str) { // LBH QVQ VG!
var decodedStr = "";
var charShift = 0;
for (var i = 0, length = str.length; i < length; i++) {
if ( /[a-m]/i.test(str[i]) ) {
charShift = 13;
} else if ( /[n-z]/i.test(str[i] ) ) {
charShift = -13;
} else {
charShift = 0;
}
if (charShift !== 0) {
decodedStr += String.fromCharCode(str.charCodeAt(i) + charShift);
} else {
decodedStr += str[i];
}
}
return decodedStr;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment