File tree 1 file changed +72
-0
lines changed
1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 2726. Calculator with Method Chaining
2
+ // URL -> https://leetcode.com/problems/calculator-with-method-chaining
3
+
4
+ class Calculator {
5
+
6
+ /**
7
+ * @param {number } value
8
+ */
9
+ constructor ( value ) {
10
+ this . result = value
11
+ }
12
+
13
+ /**
14
+ * @param {number } value
15
+ * @return {Calculator }
16
+ */
17
+ add ( value ) {
18
+ this . result += value
19
+ return this
20
+ }
21
+
22
+ /**
23
+ * @param {number } value
24
+ * @return {Calculator }
25
+ */
26
+ subtract ( value ) {
27
+ this . result -= value
28
+ return this
29
+
30
+ }
31
+
32
+ /**
33
+ * @param {number } value
34
+ * @return {Calculator }
35
+ */
36
+ multiply ( value ) {
37
+ this . result *= value
38
+ return this
39
+
40
+
41
+ }
42
+
43
+ /**
44
+ * @param {number } value
45
+ * @return {Calculator }
46
+ */
47
+ divide ( value ) {
48
+ if ( value === 0 ) {
49
+ throw new Error ( 'Division by zero is not allowed' )
50
+ }
51
+ this . result /= value
52
+ return this
53
+
54
+ }
55
+
56
+ /**
57
+ * @param {number } value
58
+ * @return {Calculator }
59
+ */
60
+ power ( value ) {
61
+ this . result = Math . pow ( this . result , value )
62
+ return this
63
+
64
+ }
65
+
66
+ /**
67
+ * @return {number }
68
+ */
69
+ getResult ( ) {
70
+ return this . result
71
+ }
72
+ }
You can’t perform that action at this time.
0 commit comments