Skip to content

Instantly share code, notes, and snippets.

@iCaspar
Created December 11, 2018 16:20
Show Gist options
  • Save iCaspar/b9d6d5f8f0686135bdf9c2d2516d03f6 to your computer and use it in GitHub Desktop.
Save iCaspar/b9d6d5f8f0686135bdf9c2d2516d03f6 to your computer and use it in GitHub Desktop.
A function to decode a ROT13 string
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