Created
May 28, 2019 19:52
-
-
Save scriptify/29a0de5a0b4a73b6b9708a2fcbcf857b to your computer and use it in GitHub Desktop.
Simple Caesar Decryption in JavaScript
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
function caesar(str, increment) { | |
const letters = 'abcdefghijklmnopqrstuvwxyz'; | |
return str | |
.toLowerCase() | |
.split('') | |
.map(c => { | |
const index = letters.indexOf(c); | |
if (index === -1) return c; | |
let newIndex = index + increment; | |
if (newIndex < 0) { | |
newIndex = letters.length - 1; | |
} else if (newIndex >= letters.length) { | |
newIndex = newIndex- letters.length; | |
} | |
return letters[newIndex]; | |
}) | |
.join(''); | |
} | |
function allCaesar(str) { | |
for (let i = 0; i < 26; i++) { | |
console.log(caesar(str, i)); | |
} | |
} | |
allCaesar('pbqr vf neg. Nanepul fubhyq gnxr bire gur jbeyq. wninfpevcg sbe gur shgher?') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment