Skip to content

Instantly share code, notes, and snippets.

@lubien
Forked from Woodsphreaker/rot13.js
Last active June 11, 2017 06:34
Show Gist options
  • Save lubien/229d52e8fe2873178bfa40f6dc2eb6b2 to your computer and use it in GitHub Desktop.
Save lubien/229d52e8fe2873178bfa40f6dc2eb6b2 to your computer and use it in GitHub Desktop.
Rot 13
const CODE_A = 65
const ALPHABET = [...Array.from({"length": 26}).keys()].map(key => String.fromCharCode(CODE_A + key))
const getCharIndex = (letter) => letter.charCodeAt(0) - CODE_A
const findNextChar = (index) => (changeTo) =>
ALPHABET[(index + changeTo) % 26]
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