snippetjavascriptTip
Implement the Caesar cipher in JavaScript
Viewed 0 times
javascriptimplementcipherthecaesar
Problem
The Caesar cipher is a simple substitution cipher, in which each letter is replaced by another letter a fixed number of positions down the alphabet. For example, with a left shift of
> [!NOTE]
>
> The following implementation only works for Latin letters (
Depending on the mode (decided by the
3, D would be replaced by A, E would become B, and so on.> [!NOTE]
>
> The following implementation only works for Latin letters (
A to Z and a to z), ignoring all other characters.Depending on the mode (decided by the
decrypt parameter), the cipher either adds or subtracts the shift from the character code of each letter, wrapping around the alphabet as needed. This is achieved by using the modulo operator (%) and the ternary operator (?).Solution
const caesarCipher = (str, shift, decrypt = false) => {
const s = decrypt ? (26 - shift) % 26 : shift;
const n = s > 0 ? s : 26 + (s % 26);
return [...str]
.map((l, i) => {
const c = str.charCodeAt(i);
if (c >= 65 && c <= 90)
return String.fromCharCode(((c - 65 + n) % 26) + 65);
if (c >= 97 && c <= 122)
return String.fromCharCode(((c - 97 + n) % 26) + 97);
return l;
})
.join('');
};
caesarCipher('Hello World!', 23); // 'Ebiil Tloia!'
caesarCipher('Hello World!', -3); // 'Ebiil Tloia!'
caesarCipher('Ebiil Tloia!', 23, true); // 'Hello World!'
caesarCipher('Ebiil Tloia!', -3, true); // 'Hello World!'>
> The following implementation only works for Latin letters (
A to Z and a to z), ignoring all other characters.Depending on the mode (decided by the
decrypt parameter), the cipher either adds or subtracts the shift from the character code of each letter, wrapping around the alphabet as needed. This is achieved by using the modulo operator (%) and the ternary operator (?).Then, using the spread operator (
...) and Array.prototype.map(), the algorithm iterates over the letters of the given string. For each letter, it converts it to its character code using String.prototype.charCodeAt(), applies the shift and converts it back to a letter using String.fromCharCode(). If the character code is not in the range 65 (A) to 90 (Z) or 97 (a) to 122 (z), it is left as is.Finally, the letters are combined into a string using
Array.prototype.join().> [!CAUTION]
Code Snippets
const caesarCipher = (str, shift, decrypt = false) => {
const s = decrypt ? (26 - shift) % 26 : shift;
const n = s > 0 ? s : 26 + (s % 26);
return [...str]
.map((l, i) => {
const c = str.charCodeAt(i);
if (c >= 65 && c <= 90)
return String.fromCharCode(((c - 65 + n) % 26) + 65);
if (c >= 97 && c <= 122)
return String.fromCharCode(((c - 97 + n) % 26) + 97);
return l;
})
.join('');
};
caesarCipher('Hello World!', 23); // 'Ebiil Tloia!'
caesarCipher('Hello World!', -3); // 'Ebiil Tloia!'
caesarCipher('Ebiil Tloia!', 23, true); // 'Hello World!'
caesarCipher('Ebiil Tloia!', -3, true); // 'Hello World!'Context
From 30-seconds-of-code: caesar-cipher
Revisions (0)
No revisions yet.