-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.js
69 lines (57 loc) · 1.76 KB
/
index.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Most simplest encryption scheme. Read more: [http://practicalcryptography.com/ciphers/caesar-cipher/]
* @param {String} str
* @param {Number} num
*/
function caesarCipher(str, num) {
if (!num) throw new Error('Missing argument: num');
const lowerCaseString = str.toLowerCase();
const alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('');
const totalAlphabets = alphabets.length;
let result = '';
// handle large number, like 300 or -300
num %= totalAlphabets;
const alphabetsMap = new Map();
for (const index in alphabets) {
if (index) {
alphabetsMap[alphabets[index]] = index;
}
}
for (const index in lowerCaseString) {
if (index) {
// get the current character
const currentCharacter = lowerCaseString[index];
// if character is space, add it to the result and continue to next
if (currentCharacter === ' ') {
result += currentCharacter;
continue;
}
// determine the new index
/**
* const currentIndex = alphabets.indexOf(currentCharacter);
*
* With indexOf complexity will be O(n*26)
* With Map complexity will be O(n).
*/
const currentIndex = Number(alphabetsMap[currentCharacter]);
let newIndex = currentIndex + num;
// if the index passes 25, restart from 0
if (newIndex > totalAlphabets - 1) {
newIndex -= totalAlphabets;
}
if (newIndex < 0) {
newIndex = totalAlphabets + newIndex;
}
// check if the character in original string was upper case
if (str[index] === str[index].toUpperCase()) {
result += alphabets[newIndex].toUpperCase();
} else {
result += alphabets[newIndex];
}
}
}
return result;
}
module.exports = {
caesarCipher,
};