Last active
June 20, 2017 11:09
-
-
Save Woodsphreaker/31e9cfbb5f36f165db4a733b97b5f61e to your computer and use it in GitHub Desktop.
Rot 13
This file contains hidden or 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
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