Last active
September 1, 2016 14:18
-
-
Save adekbadek/504f64e5753b8c15b831971870f8359f to your computer and use it in GitHub Desktop.
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 a = 'abcdefghijklmnopqrstuvwxyz' | |
const ceasar = (str, rot) => { | |
return str.split('').map((letter) => { | |
if (a.indexOf(letter.toLowerCase()) < 0) { | |
return letter | |
} | |
let rotLetter = a[(a.indexOf(letter.toLowerCase()) + rot) % a.length] | |
return letter.toLowerCase() === letter ? rotLetter : rotLetter.toUpperCase() | |
}).join('') | |
} | |
const vigenere = (str, key, direction) => { | |
key = key.replace(/[ ,\?!]/g, '') | |
let i = 0 | |
return str.split('').map((letter) => { | |
if (a.indexOf(letter.toLowerCase()) >= 0) { | |
const keyLetterInd = a.indexOf(key[i % key.length].toLowerCase()) | |
const index = (a.indexOf(letter.toLowerCase()) + (direction ? keyLetterInd : a.length - keyLetterInd)) % a.length | |
i++ | |
return letter.toLowerCase() === letter ? a[index] : a[index].toUpperCase() | |
} else { | |
return letter | |
} | |
}).join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment