cipher decode online code example
Example: caesar cipher
function caesarCipher(str, shift) {
const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split("");
let res = "";
for (let i = 0; i < str.length; i++) {
const char = str[i];
const idx = alphabetArr.indexOf(char);
// if it is not a letter, don`t shift it
if (idx === -1) {
res += char;
continue;
}
// only 26 letters in alphabet, if > 26 it wraps around
const encodedIdx = (idx + shift) % 26;
res += alphabetArr[encodedIdx];
}
return res;
}