-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
100 lines (55 loc) · 1.81 KB
/
kata.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function encrypt(text, n) {
if (!text || n <= 0) return text;
let even = '';
let odd = '';
for (let i = 0; i < text.length; i++) {
i % 2 !== 0 ? odd += text[i] : even += text[i];
}
return encrypt(`${odd}${even}`, --n);
}
function decrypt(encryptedText, n) {
if (!encryptedText || n <= 0) return encryptedText;
let even = encryptedText.slice(0, Math.floor(encryptedText.length / 2));
let odd = encryptedText.slice(Math.floor(encryptedText.length / 2));
let deccryptText = '';
for (let i = 0; i < odd.length; i++) {
deccryptText += `${odd[i]}${even[i] !== undefined ? even[i] : ''}`
}
return decrypt(deccryptText, --n);
}
console.log(encrypt("This is a test!", 0));
// "This is a test!");
console.log(encrypt("This is a test!", 1));
// "hsi etTi sats!");
console.log(encrypt("This is a test!", 2));
// "s eT ashi tist!");
console.log(encrypt("This is a test!", 3));
// " Tah itse sits!");
console.log(encrypt("This is a test!", 4));
// "This is a test!");
console.log(encrypt("This is a test!", -1));
// "This is a test!");
console.log(encrypt("This kata is very interesting!", 1));
// "hskt svr neetn!Ti aai eyitrsig");
console.log(decrypt("This is a test!", 0));
// "This is a test!");
console.log(decrypt("hsi etTi sats!", 1));
// "This is a test!");
console.log(decrypt("s eT ashi tist!", 2));
// "This is a test!");
console.log(decrypt(" Tah itse sits!", 3));
// "This is a test!");
console.log(decrypt("This is a test!", 4));
// "This is a test!");
console.log(decrypt("This is a test!", -1));
// "This is a test!");
console.log(decrypt("hskt svr neetn!Ti aai eyitrsig", 1));
// "This kata is very interesting!");
console.log(encrypt("", 0));
// "");
console.log(decrypt("", 0));
// "");
console.log(encrypt(null, 0));
// null);
console.log(decrypt(null, 0));
// null);