Created
November 17, 2016 16:54
-
-
Save misterussell/c6a9a721a34df7ba63b65424e2a73437 to your computer and use it in GitHub Desktop.
Rot13 decoder
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
function rot13(S) { | |
//create our alphabet | |
let az = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split(''); | |
//create an array for the string passed in | |
let splitS = S.split(' '); | |
//recreate the new arrays | |
let newString = splitS.map((word) => { | |
let splitWord = word.split(''); | |
let newWord = splitWord.map((letter) => { | |
// when letter = az | |
let letterIndex = az.indexOf(letter); | |
if (letterIndex > 13) { | |
return az[letterIndex + 13 - 26]; | |
} else return az[letterIndex + 13]; | |
}).join(''); | |
return newWord; | |
}).join(' '); | |
return newString; | |
}; | |
console.log(rot13('LBH QVQ VG')); // YOU DID IT | |
console.log(rot13('WOO FOR MATH')); // JBB SBE ZNGU |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment