-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2726-CalculatorWithMethodChaining.js
More file actions
65 lines (58 loc) · 1.13 KB
/
2726-CalculatorWithMethodChaining.js
File metadata and controls
65 lines (58 loc) · 1.13 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
class Calculator {
/**
* @param {number} value
*/
constructor(value) {
this.result = value;
}
/**
* @param {number} value
* @return {Calculator}
*/
add(value){
this.result += value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
subtract(value){
this.result -= value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
multiply(value) {
this.result *= value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
divide(value) {
if(value === 0) {
throw new Error("Division by zero is not allowed");
} else {
this.result /= value;
return this;
}
}
/**
* @param {number} value
* @return {Calculator}
*/
power(value) {
this.result = Math.pow(this.result, value);
return this;
}
/**
* @return {number}
*/
getResult() {
return this.result;
}
}