-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage-mixer.js
46 lines (39 loc) · 1.58 KB
/
message-mixer.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
// Import the functions from encryptors.js here.
const {caesarCipher, symbolCipher, reverseCipher} = require("./encryptors");
// User Input / Output Logic
/////////////////////////////////////////////
const encryptionMethod = getEncryptionMethod();
process.stdin.on('data', (userInput) => {
displayEncryptedMessage(encryptionMethod, userInput);
});
/* Helper function for determining which cipher method
the user chose when they ran the program. */
function getEncryptionMethod() {
let encryptionMethod;
const encryptionType = process.argv[2];
if (encryptionType === 'symbol') {
encryptionMethod = symbolCipher;
} else if (encryptionType === 'reverse') {
encryptionMethod = reverseCipher;
} else if (encryptionType === 'caesar') {
let amount = Number(process.argv[3]);
if (Number.isNaN(amount)) {
process.stdout.write(`Try again with a valid amount argument. \n`)
process.exit();
}
encryptionMethod = (str) => caesarCipher(str, amount);
}
else {
process.stdout.write(`Try again with a valid encryption type. \n`)
process.exit();
}
process.stdout.write('Enter the message you would like to encrypt...\n> ');
return encryptionMethod;
}
/* Helper function for displaying the encrypted message to the user. */
function displayEncryptedMessage(encryptionMethod, userInput) {
let str = userInput.toString().trim();
let output = encryptionMethod(str);
process.stdout.write(`\nHere is your encrypted message:\n> ${output}\n`)
process.exit();
}