Skip to content

Instantly share code, notes, and snippets.

@scriptify
Created May 28, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptify/29a0de5a0b4a73b6b9708a2fcbcf857b to your computer and use it in GitHub Desktop.
Save scriptify/29a0de5a0b4a73b6b9708a2fcbcf857b to your computer and use it in GitHub Desktop.
Simple Caesar Decryption in JavaScript
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