-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathrot13.js
More file actions
36 lines (31 loc) · 878 Bytes
/
rot13.js
File metadata and controls
36 lines (31 loc) · 878 Bytes
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
const lower = "abcdefghijklmnopqrstuvwxyz";
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const rotateChar = (char) => {
if (lower.includes(char)) {
const newIdx = (lower.indexOf(char) + 13) % lower.length;
return lower[newIdx];
}
if (upper.includes(char)) {
const newIdx = (upper.indexOf(char) + 13) % upper.length;
return upper[newIdx];
}
return char;
}
const rotate13 = (string) => {
const stringArray = string.split("");
const rotatedBy13 = stringArray.map((char) => rotateChar(char));
return rotatedBy13.join("");
}
const exit = () => {
console.log('Usage: please provide a string to encrypt');
process.exit();
}
const main = (input) => {
try {
input.length > 0 ? console.log(rotate13(input)): exit();
} catch {
exit();
}
}
main(process.argv[2]);
main(process.argv[2]);