Public
Edited
May 29, 2023
Insert cell
Insert cell
cipher = new CaesarCipher(3);
Insert cell
enc = cipher.encrypt("Hello, world!")
Insert cell
dec = cipher.decrypt(enc)
Insert cell
class CaesarCipher {
constructor(shift) {
let alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
let shifted = alphabet.map((l, i) => alphabet[(i + shift) % alphabet.length]);
alphabet = alphabet.concat(alphabet.map(l => l.toUpperCase()));
shifted = shifted.concat(shifted.map(l => l.toUpperCase()));
this.encMap = Object.fromEntries(alphabet.map((l, i) => [l, shifted[i]]));
this.decMap = Object.fromEntries(shifted.map((l, i) => [l, alphabet[i]]));
}

encrypt(plaintext) {
return plaintext.split("").map(l => this.encMap[l] || l).join("");
}

decrypt(ciphertext) {
return ciphertext.split("").map(l => this.decMap[l] || l).join("");
}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more