const caesarCipher = (str, num) => {CodeBase | Frontend | #snippets
const arr = [...'abcdefghijklmnopqrstuvwxyz']
let newStr = ''
for (const char of str) {
const lower = char.toLowerCase()
if (!arr.includes(lower)) {
newStr += char
continue
}
let index = arr.indexOf(lower) + (num % 26)
if (index > 25) index -= 26
if (index < 0) index += 26
newStr +=
char === char.toUpperCase() ? arr[index].toUpperCase() : arr[index]
}
return newStr
}
caesarCipher('Hello World', 100) // Dahhk Sknhz
caesarCipher('Dahhk Sknhz', -100) // Hello World
Please open Telegram to view this post
VIEW IN TELEGRAM