Created
December 11, 2018 16:20
-
-
Save iCaspar/b9d6d5f8f0686135bdf9c2d2516d03f6 to your computer and use it in GitHub Desktop.
A function to decode a ROT13 string
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! | |
const allCapsRegex = /[A-Z]/ | |
let decoded = '' | |
let char = 0 | |
while (char < str.length) { | |
if (true === allCapsRegex.test(str[char])) { | |
decoded += String.fromCharCode( | |
str[char].charCodeAt(0) <= 77 ? str[char].charCodeAt(0) + 13 : str[char].charCodeAt(0) - 13 | |
) | |
} else { | |
decoded += str[char] | |
} | |
char++ | |
} | |
return decoded | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment