-
-
Save lubien/229d52e8fe2873178bfa40f6dc2eb6b2 to your computer and use it in GitHub Desktop.
Rot 13
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
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