Created
November 24, 2016 01:05
-
-
Save AlexandroPerez/49e1bb227986407d6bac104f0650a71f to your computer and use it in GitHub Desktop.
Free Code Camp - Basic Algorithm Scripting - Caesars Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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