-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.js
35 lines (30 loc) · 874 Bytes
/
encode.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
const letters = "abcdefghijklmnopqrstuvwxyz";
let text = process.argv[2];
let key = process.argv[2].split(' ')[0];
let result = text.split('');
let key_text = [];
while (key_text.length < result.length) {
key_text.push(key);
}
key_text = key_text.join("").split('');
for (const [i, letter] of text.split('').entries()){
if (letters.includes(letter)) {
continue;
}
key_text.splice(i, 0, letter);
}
key_text = key_text.slice(0, text.length);
for (const [i, letter] of key_text.entries()){
if (!letters.includes(letter)){
continue;
}
if (!letters.includes(result[i])){
continue;
}
let a = letters.indexOf(result[i])+letters.indexOf(letter)+1;
while (a >= 26) {
a -= 26;
}
result[i] = letters[a]
}
console.log(key + " " + result.slice(key.length+1).join(""));