-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
56 lines (50 loc) · 1.58 KB
/
calculator.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 readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
class Calculator {
constructor(numOne, numTwo) {
this.numOne = numOne;
this.numTwo = numTwo;
}
add() {
console.log(`${this.numOne} + ${this.numTwo} =`, this.numOne + this.numTwo);
}
subtract() {
console.log(`${this.numOne} - ${this.numTwo} =`, this.numOne - this.numTwo);
}
multiply() {
console.log(`${this.numOne} * ${this.numTwo} =`, this.numOne * this.numTwo);
}
divide() {
if (this.numTwo !== 0) {
console.log(`${this.numOne} / ${this.numTwo} =`, this.numOne / this.numTwo);
} else {
console.log('Error: Division by zero');
}
}
}
readline.question('Enter first number: ', numOne => {
readline.question('Enter second number: ', numTwo => {
const calculator = new Calculator(parseFloat(numOne), parseFloat(numTwo));
readline.question('Choose operation ( + - * /): ', operation => {
switch (operation) {
case '+':
calculator.add();
break;
case '-':
calculator.subtract();
break;
case '*':
calculator.multiply();
break;
case '/':
calculator.divide();
break;
default:
console.log('Invalid operation');
}
readline.close();
});
});
});