Skip to content

Commit 1eec5b4

Browse files
committed
feat: add Caesar Cipher
1 parent 14abfd5 commit 1eec5b4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@
6363
| 059 | [Strange Counter](https://www.hackerrank.com/challenges/strange-code/problem?isFullScreen=true) | [StrangeCounter.js](./solutions-of-algorithms/StrangeCounter.js) |
6464
| 060 | [Strong Password](https://www.hackerrank.com/challenges/strong-password/problem?isFullScreen=true) | [StrongPassword.js](./solutions-of-algorithms/StrongPassword.js) |
6565
| 061 | [Flatland Space Stations](https://www.hackerrank.com/challenges/flatland-space-stations/problem?isFullScreen=true) | [FlatlandSpaceStations.js](./solutions-of-algorithms/FlatlandSpaceStations.js) |
66-
| 062 | [Insertion Sort - Part 2](https://www.hackerrank.com/challenges/insertionsort2/problem?isFullScreen=true) | [InsertionSort-Part2.js](./solutions-of-algorithms/InsertionSort-Part2.js) |
66+
| 062 | [Insertion Sort - Part 2](https://www.hackerrank.com/challenges/insertionsort2/problem?isFullScreen=true) | [InsertionSort-Part2.js](./solutions-of-algorithms/InsertionSort-Part2.js) |
67+
| 063 | [Caesar Cipher](https://www.hackerrank.com/challenges/caesar-cipher-1/problem?isFullScreen=true) | [CaesarCipher.js](./solutions-of-algorithms/CaesarCipher.js) |
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function caesarCipher(s, k) {
2+
3+
let alphabet = "abcdefghijklmnopqrstuvwxyz";
4+
let ucAlphabet = alphabet.toUpperCase();
5+
let result = [];
6+
for (let key of s) {
7+
if (ucAlphabet.includes(key)) {
8+
let a = ucAlphabet.indexOf(key);
9+
if ((a + k) >= ucAlphabet.length) {
10+
result.push(ucAlphabet[((a + k) % 26)]);
11+
} else {
12+
result.push(ucAlphabet[a + k]);
13+
}
14+
} else if (alphabet.includes(key)) {
15+
let a = alphabet.indexOf(key);
16+
if ((a + k) >= alphabet.length) {
17+
result.push(alphabet[((a + k) % 26)]);
18+
} else {
19+
result.push(alphabet[a + k]);
20+
}
21+
} else {
22+
result.push(key);
23+
}
24+
}
25+
return result.join('');
26+
}

0 commit comments

Comments
 (0)