-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-kyu-rot13-1.js
61 lines (44 loc) · 1.66 KB
/
5-kyu-rot13-1.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
// 5 kyu | Rot13
// https://www.codewars.com/kata/rot13-1/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// const upper = [...new Array(26).keys()].map(n => String.fromCharCode(n + 65));
// const lower = [...new Array(26).keys()].map(n => String.fromCharCode(n + 97));
// const upperRotated = [...upper];
// const lowerRotated = [...lower];
// const map = new Map();
// for (let i = 0; i < 13; i++) {
// [upper[i], upper[i + 13]] = [upper[i + 13], upper[i]];
// [lower[i], lower[i + 13]] = [lower[i + 13], lower[i]];
// }
// for (let i = 0; i < 26; i++) {
// map.set(upper[i], upperRotated[i]);
// map.set(lower[i], lowerRotated[i]);
// }
// const rot13 = message =>
// [...message].map(c => (map.has(c) ? map.get(c) : c)).join('');
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const rotatedCharCodes = [...new Array(128).keys()];
for (let i = 0; i < 13; i++) {
[
rotatedCharCodes[65 + i],
rotatedCharCodes[65 + i + 13],
rotatedCharCodes[97 + i],
rotatedCharCodes[97 + i + 13],
] = [
rotatedCharCodes[65 + i + 13],
rotatedCharCodes[65 + i],
rotatedCharCodes[97 + i + 13],
rotatedCharCodes[97 + i],
];
}
const rot13 = message =>
[...message]
.map(c => String.fromCharCode(rotatedCharCodes[c.charCodeAt(0)]))
.join('');
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
console.log(rot13('a-b-c'));
console.log(rot13('test'));
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// import { strictEqual } from 'assert';
// strictEqual(rot13('test'), 'grfg');
// strictEqual(rot13('Test'), 'Grfg');