Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active June 20, 2017 11:09
Show Gist options
  • Save Woodsphreaker/31e9cfbb5f36f165db4a733b97b5f61e to your computer and use it in GitHub Desktop.
Save Woodsphreaker/31e9cfbb5f36f165db4a733b97b5f61e to your computer and use it in GitHub Desktop.
Rot 13
const ALPHABET = Array.from({"length": 26} , (_ , char) => String.fromCharCode(65 + char))
const getCharIndex = (letter) => ALPHABET.indexOf(letter)
const findNextChar = (index) => (changeTo) => {
return index >= 13
? ALPHABET[index - changeTo]
: ALPHABET[index + changeTo]
}
const getChar = (char) => (changeTo = 0) => {
return getCharIndex(char) >= 0
? findNextChar(getCharIndex(char))(changeTo)
: char
}
const rot = (str) => (changeTo) => str.split('')
.reduce((acc, cur) => acc + getChar(cur)(changeTo), '')
console.log(rot('ABCDEFGHIJKLMNOPQRSTUVWXYZ')(13)) //NOPQRSTUVWXYZABCDEFGHIJKLM
console.log(rot('LBH QVQ VG!')(13)) //YOU DID IT!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment