Elffuss BraidHub

kikocis/continue public

main · 2.1 KiBRawPermalink
1class Calculator {
2 constructor() {
3 this.result = 0;
4 }
5
6 add(number) {
7 this.result += number;
8 return this;
9 }
10
11 subtract(number) {
12 this.result -= number;
13 return this;
14 }
15
16 multiply(number) {
17 this.result *= number;
18 return this;
19 }
20
21 divide(number) {
22 if (number === 0) {
23 throw new Error("Cannot divide by zero");
24 }
25 this.result /= number;
26 return this;
27 }
28
29 getResult() {
30 return this.result;
31 }
32
33 reset() {
34 this.result = 0;
35 return this;
36 }
37}
38
39---
40
41class Calculator {
42 // ... existing code ...
43
44 // Adds the given number to the result
45 add(number) {
46 this.result += number;
47 return this;
48 }
49
50 // Subtracts the given number from the result
51 subtract(number) {
52 this.result -= number;
53 return this;
54 }
55
56 // Multiplies the result by the given number
57 multiply(number) {
58 this.result *= number;
59 return this;
60 }
61
62 // Divides the result by the given number
63 // Throws an error if attempting to divide by zero
64 divide(number) {
65 if (number === 0) {
66 throw new Error("Cannot divide by zero");
67 }
68 this.result /= number;
69 return this;
70 }
71
72 // Returns the current result
73 getResult() {
74 return this.result;
75 }
76
77 // Resets the result to zero
78 reset() {
79 this.result = 0;
80 return this;
81 }
82}
83
84---
85
86class Calculator {
87 constructor() {
88 this.result = 0;
89 }
90
91+ // Adds the given number to the result
92 add(number) {
93 this.result += number;
94 return this;
95 }
96
97+ // Subtracts the given number from the result
98 subtract(number) {
99 this.result -= number;
100 return this;
101 }
102
103+ // Multiplies the result by the given number
104 multiply(number) {
105 this.result *= number;
106 return this;
107 }
108
109+ // Divides the result by the given number
110+ // Throws an error if attempting to divide by zero
111 divide(number) {
112 if (number === 0) {
113 throw new Error("Cannot divide by zero");
114 }
115 this.result /= number;
116 return this;
117 }
118
119+ // Returns the current result
120 getResult() {
121 return this.result;
122 }
123
124+ // Resets the result to zero
125 reset() {
126 this.result = 0;
127 return this;
128 }
129}