| 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 | class Calculator { |
| 42 | // ... existing code ... |
| 43 | |
| 44 | static add(a, b) { |
| 45 | return a + b; |
| 46 | } |
| 47 | |
| 48 | static subtract(a, b) { |
| 49 | return a - b; |
| 50 | } |
| 51 | |
| 52 | static multiply(a, b) { |
| 53 | return a * b; |
| 54 | } |
| 55 | |
| 56 | static divide(a, b) { |
| 57 | if (b === 0) { |
| 58 | throw new Error("Cannot divide by zero"); |
| 59 | } |
| 60 | return a / b; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | --- |
| 65 | |
| 66 | class Calculator { |
| 67 | constructor() { |
| 68 | this.result = 0; |
| 69 | } |
| 70 | |
| 71 | - add(number) { |
| 72 | - this.result += number; |
| 73 | - return this; |
| 74 | + static add(a, b) { |
| 75 | + return a + b; |
| 76 | } |
| 77 | |
| 78 | - subtract(number) { |
| 79 | - this.result -= number; |
| 80 | - return this; |
| 81 | + static subtract(a, b) { |
| 82 | + return a - b; |
| 83 | } |
| 84 | |
| 85 | - multiply(number) { |
| 86 | - this.result *= number; |
| 87 | - return this; |
| 88 | + static multiply(a, b) { |
| 89 | + return a * b; |
| 90 | } |
| 91 | |
| 92 | - divide(number) { |
| 93 | - if (number === 0) { |
| 94 | + static divide(a, b) { |
| 95 | + if (b === 0) { |
| 96 | throw new Error("Cannot divide by zero"); |
| 97 | } |
| 98 | - this.result /= number; |
| 99 | - return this; |
| 100 | + return a / b; |
| 101 | } |
| 102 | - |
| 103 | - getResult() { |
| 104 | - return this.result; |
| 105 | - } |
| 106 | - |
| 107 | - reset() { |
| 108 | - this.result = 0; |
| 109 | - return this; |
| 110 | - } |
| 111 | } |