-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
56 lines (48 loc) · 1.79 KB
/
calc.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
const Operations = require('./operations');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(`
Calc.js
Welcome to the Node.js Calculator app
Version 1.0.0
Usage: The user will be prompted for two numbers,
then asked to select their operation of choice.
`);
rl.question('Enter your first number: ', x => {
rl.question('Enter your second number: ', y => {
rl.question(
`Please select from the following options:
[1] Addition (+)
[2] Subtraction (-)
[3] Multiplicaiton (*)
[4] Division (/)
Enter your choice: `, choice => {
[x, y] = [+x, +y];
if (Operations.validateNumbers(x, y)) {
switch (choice) {
case '1':
console.log(`The sum of ${x} and ${y} is ${Operations.add(x, y)}`);
break;
case '2':
console.log(`The subtract of ${x} and ${y} is ${Operations.subtract(x, y)}`);
break;
case '3':
console.log(`The product of ${x} and ${y} is ${Operations.multiply(x, y)}`);
break;
case '4':
console.log(`The quotient of ${x} and ${y} is ${Operations.divide(x, y)}`);
break;
default:
console.log('Please restart the program and select a number between 1 and 4');
break;
}
} else {
console.log('Only numbers are allowed! Please restart the program.');
}
rl.close();
});
})
});