-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.js
More file actions
26 lines (25 loc) · 810 Bytes
/
CaesarCipher.js
File metadata and controls
26 lines (25 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function caesarCipher(s, k) {
let alphabet = "abcdefghijklmnopqrstuvwxyz";
let ucAlphabet = alphabet.toUpperCase();
let result = [];
for (let key of s) {
if (ucAlphabet.includes(key)) {
let a = ucAlphabet.indexOf(key);
if ((a + k) >= ucAlphabet.length) {
result.push(ucAlphabet[((a + k) % 26)]);
} else {
result.push(ucAlphabet[a + k]);
}
} else if (alphabet.includes(key)) {
let a = alphabet.indexOf(key);
if ((a + k) >= alphabet.length) {
result.push(alphabet[((a + k) % 26)]);
} else {
result.push(alphabet[a + k]);
}
} else {
result.push(key);
}
}
return result.join('');
}