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