-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator.js
More file actions
72 lines (63 loc) · 1.79 KB
/
calculator.js
File metadata and controls
72 lines (63 loc) · 1.79 KB
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
62
63
64
65
66
67
68
69
70
71
72
class Calculator {
constructor() {
this.history = [];
}
add(...numbers) {
const result = numbers.reduce((sum, num) => sum + num, 0);
this.history.push({ operation: "add", numbers, result });
return result;
}
subtract(...numbers) {
const result = numbers.reduce((diff, num, idx) =>
idx === 0 ? num : diff - num
);
this.history.push({ operation: "subtract", numbers, result });
return result;
}
multiply(...numbers) {
const result = numbers.reduce((product, num) => product * num, 1);
this.history.push({ operation: "multiply", numbers, result });
return result;
}
divide(...numbers) {
if (numbers.slice(1).includes(0)) {
throw new Error("Division by zero is not allowed");
}
const result = numbers.reduce((quotient, num, idx) =>
idx === 0 ? num : quotient / num
);
this.history.push({ operation: "divide", numbers, result });
return result;
}
power(base, exponent) {
const result = Math.pow(base, exponent);
this.history.push({
operation: "power",
numbers: [base, exponent],
result,
});
return result;
}
sqrt(number) {
if (number < 0) {
throw new Error("Cannot calculate square root of negative number");
}
const result = Math.sqrt(number);
this.history.push({ operation: "sqrt", numbers: [number], result });
return result;
}
getHistory() {
return this.history;
}
clearHistory() {
this.history = [];
}
}
const calc = new Calculator();
console.log(calc.add(1, 2, 3)); // 6
console.log(calc.subtract(10, 5, 3)); // 2
console.log(calc.multiply(2, 3, 4)); // 24
console.log(calc.divide(100, 2, 2)); // 25
console.log(calc.power(2, 3)); // 8
console.log(calc.sqrt(16)); // 4
console.log(calc.getHistory()); // Shows operation history