Caesar Cipher Encryptor

  • n, n

const alphabet = `abcdefghijklmnopqrstuvwxyz`;
const aCode = alphabet.charCodeAt(0);
function caesarCipherEncryptor(str, key) {
  const answer = [];
  for (let i = 0; i < str.length; i++) {
    let code = str.charCodeAt(i) - aCode;
    code += key;
    code %= 26;
    answer.push(alphabet[code]);
  }

  return answer.join('');
}

// Do not edit the line below.
exports.caesarCipherEncryptor = caesarCipherEncryptor;

Last updated