Skip to content

Commit 76b9ace

Browse files
committed
--update: final implementation
1 parent c801b3e commit 76b9ace

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/_Classics_/caeser_cipher/index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
/**
2+
* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/]
3+
* @param {String} str
4+
* @param {Number} num
5+
*/
6+
17
function caeserCipher(str, num) {
28
const lowerCaseString = str.toLowerCase();
39
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
410
const totalAlphabets = alphabets.length;
511
let result = '';
612

13+
// handle large number, like 300 or -300
14+
num %= totalAlphabets;
15+
716
for (let index in lowerCaseString) {
817
// get the current character
918
const currentCharacter = lowerCaseString[index];
@@ -28,12 +37,11 @@ function caeserCipher(str, num) {
2837
}
2938

3039
// check if the character in original string was upper case
31-
if (str[index] === alphabets[currentIndex].toUpperCase()) {
40+
if (str[index] === str[index].toUpperCase()) {
3241
result += alphabets[newIndex].toUpperCase();
42+
} else {
43+
result += alphabets[newIndex];
3344
}
34-
result += alphabets[newIndex];
3545
}
3646
return result;
3747
}
38-
39-
console.log(caeserCipher('abCz', 2));

0 commit comments

Comments
 (0)