-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar-cypher.js
40 lines (35 loc) · 1.15 KB
/
caesar-cypher.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
const movingShift = (str, shift) => {
let output = '';
for (let i = 0; i < str.length; i++) {
let current = str[i];
if (current.match(/[a-z]/i)) {
const code = str.charCodeAt(i);
if ((code >= 65) && (code <= 90))
current = String.fromCharCode(((code - 65 + shift) % 26) + 65);
else if ((code >= 97) && (code <= 122))
current = String.fromCharCode(((code - 97 + shift) % 26) + 97);
}
output += current;
shift++;
}
const rangeEnd = Math.ceil(output.length / 5);
const answer = output.match(new RegExp('.{1,' + rangeEnd + '}', 'g'));
return answer.length === 5 ? answer : answer.concat('');
};
const demovingShift = (input, shift) => {
let output = '';
let str = input.join('');
for (let i = 0; i < str.length; i++) {
let current = str[i];
if (current.match(/[a-z]/i)) {
const code = str.charCodeAt(i);
if ((code >= 65 - shift) && (code <= 90))
current = String.fromCharCode(((code - 65 - shift + (26 * input.length)) % 26) + 65);
else if ((code >= 97 - shift) && (code <= 122))
current = String.fromCharCode(((code - 97 - shift + (26 * input.length)) % 26) + 97);
}
output += current;
shift++;
}
return output;
};