| 1 | class Calculator { |
| 2 | constructor() { |
| 3 | this.result = 0; |
| 4 | } |
| 5 | } |
| 6 | |
| 7 | class Subtract extends Calculator { |
| 8 | sub(num1, num2) { |
| 9 | this.result = num1 - num2; |
| 10 | return this.result; |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | class Add extends Calculator { |
| 15 | add(num1, num2) { |
| 16 | this.result = num1 + num2; |
| 17 | return this.result; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | --- |
| 22 | |
| 23 | // ... existing code ... |
| 24 | |
| 25 | class Subtract extends Calculator { |
| 26 | sub(num1, num2) { |
| 27 | this.result = num1 - num2; |
| 28 | |
| 29 | if (this.result < 0) { |
| 30 | throw new Error("Result cannot be negative"); |
| 31 | } |
| 32 | |
| 33 | return this.result; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // ... existing code ... |
| 38 | |
| 39 | --- |
| 40 | |
| 41 | class Calculator { |
| 42 | constructor() { |
| 43 | this.result = 0; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | class Subtract extends Calculator { |
| 48 | sub(num1, num2) { |
| 49 | this.result = num1 - num2; |
| 50 | + |
| 51 | + if (this.result < 0) { |
| 52 | + throw new Error("Result cannot be negative"); |
| 53 | + } |
| 54 | + |
| 55 | return this.result; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | class Add extends Calculator { |
| 60 | add(num1, num2) { |
| 61 | this.result = num1 + num2; |
| 62 | return this.result; |
| 63 | } |
| 64 | } |