| 8 | 8 | * @param {...number} sides |
| 9 | 9 | */ |
| 10 | 10 | constructor(...sides) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Edge Case: Invalid number of sides (not exactly 3) |
| 12 | if (sides.length !== 3) { |
| 13 | throw new Error('Triangle must have exactly 3 sides'); |
| 14 | } |
| 15 | |
| 16 | // Edge Case: Non-numeric values |
| 17 | if (sides.some(side => typeof side !== 'number')) { |
| 18 | throw new Error('All sides must be numbers'); |
| 19 | } |
| 20 | |
| 21 | // Edge Case: NaN values |
| 22 | if (sides.some(side => isNaN(side))) { |
| 23 | throw new Error('Sides cannot be NaN'); |
| 24 | } |
| 25 | |
| 26 | // Edge Case: Infinite values |
| 27 | if (sides.some(side => !isFinite(side))) { |
| 28 | throw new Error('Sides cannot be infinite'); |
| 29 | } |
| 30 | |
| 31 | this.sides = sides; |
| 12 | 32 | } |
| 13 | 33 | |
| 14 | 34 | /** |
| 15 | 35 | * @returns {boolean} |
| 16 | 36 | */ |
| 17 | 37 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 38 | // Edge Case: Invalid triangle (sides <= 0) |
| 39 | if (this.sides.some(side => side <= 0)) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Triangle inequality violation |
| 44 | if (!this._isValidTriangle()) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | const [a, b, c] = this.sides; |
| 49 | return a === b && b === c; |
| 19 | 50 | } |
| 20 | 51 | |
| 21 | 52 | /** |
| 22 | 53 | * @returns {boolean} |
| 23 | 54 | */ |
| 24 | 55 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 56 | // Edge Case: Invalid triangle (sides <= 0) |
| 57 | if (this.sides.some(side => side <= 0)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Triangle inequality violation |
| 62 | if (!this._isValidTriangle()) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | const [a, b, c] = this.sides; |
| 67 | return a === b || b === c || a === c; |
| 26 | 68 | } |
| 27 | 69 | |
| 28 | 70 | /** |
| 29 | 71 | * @returns {boolean} |
| 30 | 72 | */ |
| 31 | 73 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 74 | // Edge Case: Invalid triangle (sides <= 0) |
| 75 | if (this.sides.some(side => side <= 0)) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Edge Case: Triangle inequality violation |
| 80 | if (!this._isValidTriangle()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | const [a, b, c] = this.sides; |
| 85 | return a !== b && b !== c && a !== c; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Helper method to check if the sides form a valid triangle |
| 90 | * @returns {boolean} |
| 91 | */ |
| 92 | _isValidTriangle() { |
| 93 | const [a, b, c] = this.sides; |
| 94 | |
| 95 | // All sides must be greater than 0 |
| 96 | if (a <= 0 || b <= 0 || c <= 0) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Triangle inequality: sum of any two sides must be >= third side |
| 101 | return (a + b >= c) && (b + c >= a) && (a + c >= b); |
| 33 | 102 | } |
| 34 | 103 | } |
| 104 | |
| 105 | // Handled Edge Cases: Invalid number of sides, non-numeric values, NaN values, infinite values, sides <= 0, triangle inequality violation |