-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
58 lines (53 loc) · 1.63 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
57
58
/**
* A simple Node.js calculator app that uses
* the built-in Readline command line interface.
*/
const operations = require('./operations.js');
const readline = require('readline');
// readline for creating command line interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(`
Welcome to the Node.js Calculator app!
Version: 1.0.0.
Author: Md. Jamal Uddin
Published: August 12, 2018
Usage: Please enter any two integer number
then select operation by your choice.
`);
rl.question('Enter the first number: ', (x) => {
rl.question('Enter the second number: ', (y) => {
rl.question(`
Please select from the following options:
[1] Addition (+)
[2] Subtraction (-)
[3] Multiplication (*)
[4] Division (/)
Enter your choice: `, (choice) => {
if (!operations.validateNumbers(x, y)) {
console.log('Only numbers are allowed! Please restart the program.');
} else {
switch (choice) {
case '1':
console.log(`The sum of ${x} and ${y} is ${operations.add(x, y)}.`);
break;
case '2':
console.log(`The difference 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;
}
}
rl.close();
});
});
});