-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2726-CalculatorWithMethodChaining.js
More file actions
117 lines (100 loc) · 3.66 KB
/
2726-CalculatorWithMethodChaining.js
File metadata and controls
117 lines (100 loc) · 3.66 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 2726. Calculator with Method Chaining
// Design a Calculator class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation.
// It should also allow consecutive operations to be performed using method chaining.
// The Calculator class constructor should accept a number which serves as the initial value of result.
// Your Calculator class should have the following methods:
// add - This method adds the given number value to the result and returns the updated Calculator.
// subtract - This method subtracts the given number value from the result and returns the updated Calculator.
// multiply - This method multiplies the result by the given number value and returns the updated Calculator.
// divide - This method divides the result by the given number value and returns the updated Calculator. If the passed value is 0, an error "Division by zero is not allowed" should be thrown.
// power - This method raises the result to the power of the given number value and returns the updated Calculator.
// getResult - This method returns the result.
// Solutions within 10-5 of the actual result are considered correct.
// Example 1:
// Input:
// actions = ["Calculator", "add", "subtract", "getResult"],
// values = [10, 5, 7]
// Output: 8
// Explanation:
// new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
// Example 2:
// Input:
// actions = ["Calculator", "multiply", "power", "getResult"],
// values = [2, 5, 2]
// Output: 100
// Explanation:
// new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
// Example 3:
// Input:
// actions = ["Calculator", "divide", "getResult"],
// values = [20, 0]
// Output: "Division by zero is not allowed"
// Explanation:
// new Calculator(20).divide(0).getResult() // 20 / 0
// The error should be thrown because we cannot divide by zero.
// Constraints:
// actions is a valid JSON array of strings
// values is a valid JSON array of numbers
// 2 <= actions.length <= 2 * 10^4
// 1 <= values.length <= 2 * 10^4 - 1
// actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"
// First action is always "Calculator"
// Last action is always "getResult"
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");
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;
}
}
console.log(new Calculator(10).add(5).subtract(7).getResult()) // 10 + 5 - 7 = 8
console.log(new Calculator(2).multiply(5).power(2).getResult()) // (2 * 5) ^ 2 = 100
console.log(new Calculator(20).divide(0).getResult()) // 20 / 0 Output: "Division by zero is not allowed"