Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 032ba2f

Browse files
committed
Add solution #2726
1 parent a44e53a commit 032ba2f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* 2726. Calculator with Method Chaining
3+
* https://leetcode.com/problems/calculator-with-method-chaining/
4+
* Difficulty: Easy
5+
*
6+
* Design a Calculator class. The class should provide the mathematical operations of addition,
7+
* subtraction, multiplication, division, and exponentiation. It should also allow consecutive
8+
* operations to be performed using method chaining. The Calculator class constructor should
9+
* accept a number which serves as the initial value of result.
10+
*
11+
* Your Calculator class should have the following methods:
12+
* - add - This method adds the given number value to the result and returns the updated Calculator.
13+
* - subtract - This method subtracts the given number value from the result and returns the
14+
* updated Calculator.
15+
* - multiply - This method multiplies the result by the given number value and returns the
16+
* updated Calculator.
17+
* - divide - This method divides the result by the given number value and returns the updated
18+
* Calculator. If the passed value is 0, an error "Division by zero is not allowed" should
19+
* be thrown.
20+
* - power - This method raises the result to the power of the given number value and returns
21+
* the updated Calculator.
22+
* - getResult - This method returns the result.
23+
*
24+
* Solutions within 10-5 of the actual result are considered correct.
25+
*/
26+
27+
class Calculator {
28+
/**
29+
* @param {number} value
30+
*/
31+
constructor(value) {
32+
this.value = value;
33+
}
34+
35+
/**
36+
* @param {number} value
37+
* @return {Calculator}
38+
*/
39+
add(value) {
40+
this.value += value;
41+
return this;
42+
}
43+
44+
/**
45+
* @param {number} value
46+
* @return {Calculator}
47+
*/
48+
subtract(value) {
49+
this.value -= value;
50+
return this;
51+
}
52+
53+
/**
54+
* @param {number} value
55+
* @return {Calculator}
56+
*/
57+
multiply(value) {
58+
this.value *= value;
59+
return this;
60+
}
61+
62+
/**
63+
* @param {number} value
64+
* @return {Calculator}
65+
*/
66+
divide(value) {
67+
if (value === 0) {
68+
throw new Error('Division by zero is not allowed');
69+
}
70+
this.value /= value;
71+
return this;
72+
}
73+
74+
/**
75+
* @param {number} value
76+
* @return {Calculator}
77+
*/
78+
power(value) {
79+
this.value **= value;
80+
return this;
81+
}
82+
83+
/**
84+
* @return {number}
85+
*/
86+
getResult() {
87+
return this.value;
88+
}
89+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@
488488
2722|[Join Two Arrays by ID](./2722-join-two-arrays-by-id.js)|Medium|
489489
2723|[Add Two Promises](./2723-add-two-promises.js)|Easy|
490490
2724|[Sort By](./2724-sort-by.js)|Easy|
491+
2726|[Calculator with Method Chaining](./2726-calculator-with-method-chaining.js)|Easy|
491492
2727|[Is Object Empty](./2727-is-object-empty.js)|Easy|
492493
3042|[Count Prefix and Suffix Pairs I](./3042-count-prefix-and-suffix-pairs-i.js)|Easy|
493494
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|

0 commit comments

Comments
 (0)