We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5aab5cc commit 12f9ba2Copy full SHA for 12f9ba2
src/_Classics_/caeser_cipher/index.js
@@ -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