-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
29 lines (26 loc) · 961 Bytes
/
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
function rot13(str) {
let abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let rot = str.split('')
.map((i, index) => {
let idx = abc.indexOf(i.toLowerCase());
if (idx === -1) return i;
if (idx >= 0 && idx < 13) {
if (str[index] === abc[idx]) {
return abc[idx + 13];
} else {
return abc[idx + 13].toUpperCase();
}
} else {
if (str[index] === abc[idx]) {
return abc[idx - 13];
} else {
return abc[idx - 13].toUpperCase();
}
}
}).join('');
return rot;
}
console.log(rot13("EBG13 rknzcyr."));
// "ROT13 example."
console.log(rot13("This is my first ROT13 excercise!"));
// "Guvf vf zl svefg EBG13 rkprepvfr!"