| 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 | * Divides the current result by the given number |
| 43 | * @param {number} number - The number to divide by |
| 44 | * @throws {Error} If attempting to divide by zero |
| 45 | * @returns {Calculator} The calculator instance for chaining |
| 46 | */ |
| 47 | divide(number) { |
| 48 | if (number === 0) { |
| 49 | throw new Error("Cannot divide by zero"); |
| 50 | } |
| 51 | this.result /= number; |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | --- |
| 56 | |
| 57 | class Calculator { |
| 58 | constructor() { |
| 59 | this.result = 0; |
| 60 | } |
| 61 | |
| 62 | add(number) { |
| 63 | this.result += number; |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | subtract(number) { |
| 68 | this.result -= number; |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | multiply(number) { |
| 73 | this.result *= number; |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | + /** |
| 78 | + * Divides the current result by the given number |
| 79 | + * @param {number} number - The number to divide by |
| 80 | + * @throws {Error} If attempting to divide by zero |
| 81 | + * @returns {Calculator} The calculator instance for chaining |
| 82 | + */ |
| 83 | divide(number) { |
| 84 | if (number === 0) { |
| 85 | throw new Error("Cannot divide by zero"); |
| 86 | } |
| 87 | this.result /= number; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | getResult() { |
| 92 | return this.result; |
| 93 | } |
| 94 | |
| 95 | reset() { |
| 96 | this.result = 0; |
| 97 | return this; |
| 98 | } |
| 99 | } |