Skip to content

Instantly share code, notes, and snippets.

@adekbadek
Last active September 1, 2016 14:18
Show Gist options
  • Save adekbadek/504f64e5753b8c15b831971870f8359f to your computer and use it in GitHub Desktop.
Save adekbadek/504f64e5753b8c15b831971870f8359f to your computer and use it in GitHub Desktop.
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