Skip to content

Commit 12f9ba2

Browse files
committed
--update: initial implementation
1 parent 5aab5cc commit 12f9ba2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/_Classics_/caeser_cipher/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function caeserCipher(str, num) {
2+
const lowerCaseString = str.toLowerCase();
3+
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
4+
let result = '';
5+
6+
for (let char of lowerCaseString) {
7+
const current = char;
8+
if (current === ' ') {
9+
result += current;
10+
continue;
11+
}
12+
13+
const currentIndex = alphabets.indexOf(current);
14+
let newIndex = currentIndex + num;
15+
16+
if (newIndex > alphabets.length - 1) {
17+
newIndex -= alphabets.length;
18+
}
19+
result += alphabets[newIndex];
20+
}
21+
return result;
22+
}
23+
24+
console.log(caeserCipher('abcz', 2));

0 commit comments

Comments
 (0)