-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
61 lines (54 loc) · 1.61 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
59
60
61
//create a CLI for user
const operations = require('./operations.js');
const readline = require('readline');
//use realine to create command line interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(`
Calc.js
Welcome to the Nodejs Calculator app!
V: 1.0.0.
Usage: The user will be prompted for two numbers,
then asked to select their operation of 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()
}
)
})
})
//exit
rl.close();