forked from csvenja/javascript-exercises
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencrypt-decrypt.js
More file actions
30 lines (22 loc) · 1.09 KB
/
encrypt-decrypt.js
File metadata and controls
30 lines (22 loc) · 1.09 KB
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
function encrypt(text, n) {
if (!text || n <= 0) {
return text;
}
const even = text.split('').filter((item, i) => i%2 === 0).join('');
const odd = text.split('').filter((item, i) => i%2 !== 0).join('');
const res = (odd+even).toLowerCase();
return (n - 1) ? encrypt(res, n-1) : res;
}
function decrypt(text, n) {
if (!text || n <= 0) {
return text;
}
const even = text.slice(0, text.length / 2).split('');
const odd = text.slice(text.length / 2).split('');
const res = odd.map((item, i) => item+even[i]).join('');
return (n - 1) ? decrypt(res, n-1) : res;
}
console.log("[encrypt] Abcdefghij => bdfhjacegi", encrypt('Abcdefghij', 1) === 'bdfhjacegi')
console.log("[encrypt] Abcdefghij => bdfhjacegi => dhaeibfjcg", encrypt('Abcdefghij', 2) === 'dhaeibfjcg')
console.log("[decrypt] bdfhjacegi => abcdefghij", decrypt('bdfhjacegi', 1) === 'abcdefghij')
console.log("[decrypt] dhaeibfjcg => bdfhjacegi => abcdefghij", decrypt('dhaeibfjcg', 2) === 'abcdefghij')