| 1 | class 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 | |
| 41 | /** |
| 42 | * Calculator class for performing basic arithmetic operations. |
| 43 | */ |
| 44 | class Calculator { |
| 45 | /** |
| 46 | * Creates a new Calculator instance. |
| 47 | * Initializes the result to 0. |
| 48 | */ |
| 49 | constructor() { |
| 50 | this.result = 0; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Adds a number to the current result. |
| 55 | * @param {number} number - The number to add. |
| 56 | * @returns {Calculator} The Calculator instance for method chaining. |
| 57 | */ |
| 58 | add(number) { |
| 59 | this.result += number; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Subtracts a number from the current result. |
| 65 | * @param {number} number - The number to subtract. |
| 66 | * @returns {Calculator} The Calculator instance for method chaining. |
| 67 | */ |
| 68 | subtract(number) { |
| 69 | this.result -= number; |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Multiplies the current result by a number. |
| 75 | * @param {number} number - The number to multiply by. |
| 76 | * @returns {Calculator} The Calculator instance for method chaining. |
| 77 | */ |
| 78 | multiply(number) { |
| 79 | this.result *= number; |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Divides the current result by a number. |
| 85 | * @param {number} number - The number to divide by. |
| 86 | * @returns {Calculator} The Calculator instance for method chaining. |
| 87 | * @throws {Error} If attempting to divide by zero. |
| 88 | */ |
| 89 | divide(number) { |
| 90 | if (number === 0) { |
| 91 | throw new Error("Cannot divide by zero"); |
| 92 | } |
| 93 | this.result /= number; |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the current result. |
| 99 | * @returns {number} The current result. |
| 100 | */ |
| 101 | getResult() { |
| 102 | return this.result; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Resets the calculator's result to 0. |
| 107 | * @returns {Calculator} The Calculator instance for method chaining. |
| 108 | */ |
| 109 | reset() { |
| 110 | this.result = 0; |
| 111 | return this; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | --- |
| 116 | |
| 117 | + /** |
| 118 | + * Calculator class for performing basic arithmetic operations. |
| 119 | + */ |
| 120 | class Calculator { |
| 121 | + /** |
| 122 | + * Creates a new Calculator instance. |
| 123 | + * Initializes the result to 0. |
| 124 | + */ |
| 125 | constructor() { |
| 126 | this.result = 0; |
| 127 | } |
| 128 | |
| 129 | + /** |
| 130 | + * Adds a number to the current result. |
| 131 | + * @param {number} number - The number to add. |
| 132 | + * @returns {Calculator} The Calculator instance for method chaining. |
| 133 | + */ |
| 134 | add(number) { |
| 135 | this.result += number; |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | + /** |
| 140 | + * Subtracts a number from the current result. |
| 141 | + * @param {number} number - The number to subtract. |
| 142 | + * @returns {Calculator} The Calculator instance for method chaining. |
| 143 | + */ |
| 144 | subtract(number) { |
| 145 | this.result -= number; |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | + /** |
| 150 | + * Multiplies the current result by a number. |
| 151 | + * @param {number} number - The number to multiply by. |
| 152 | + * @returns {Calculator} The Calculator instance for method chaining. |
| 153 | + */ |
| 154 | multiply(number) { |
| 155 | this.result *= number; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | + /** |
| 160 | + * Divides the current result by a number. |
| 161 | + * @param {number} number - The number to divide by. |
| 162 | + * @returns {Calculator} The Calculator instance for method chaining. |
| 163 | + * @throws {Error} If attempting to divide by zero. |
| 164 | + */ |
| 165 | divide(number) { |
| 166 | if (number === 0) { |
| 167 | throw new Error("Cannot divide by zero"); |
| 168 | } |
| 169 | this.result /= number; |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | + /** |
| 174 | + * Returns the current result. |
| 175 | + * @returns {number} The current result. |
| 176 | + */ |
| 177 | getResult() { |
| 178 | return this.result; |
| 179 | } |
| 180 | |
| 181 | + /** |
| 182 | + * Resets the calculator's result to 0. |
| 183 | + * @returns {Calculator} The Calculator instance for method chaining. |
| 184 | + */ |
| 185 | reset() { |
| 186 | this.result = 0; |
| 187 | return this; |
| 188 | } |
| 189 | } |