| 5 | 5 | |
| 6 | 6 | export class Triangle { |
| 7 | 7 | /** |
| 8 | | * @param {...number} sides |
| 8 | * Creates a Triangle instance with the given side lengths. |
| 9 | * @param {...number} sides - The lengths of the triangle's sides. |
| 9 | 10 | */ |
| 10 | 11 | constructor(...sides) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Invalid number of sides (not exactly 3) |
| 13 | if (sides.length !== 3) { |
| 14 | this.sides = []; |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | this.sides = sides; |
| 12 | 19 | } |
| 13 | 20 | |
| 14 | 21 | /** |
| 15 | | * @returns {boolean} |
| 22 | * Checks if the triangle is equilateral (all sides equal). |
| 23 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 24 | * Handled Edge Cases: Invalid triangle, all sides equal, not all sides equal |
| 16 | 25 | */ |
| 17 | 26 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 27 | // Edge Case: Not a valid triangle (sides <= 0 or violate triangle inequality) |
| 28 | if (!this._isValidTriangle()) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | const [a, b, c] = this.sides; |
| 33 | |
| 34 | // Edge Case: All sides are equal |
| 35 | return a === b && b === c; |
| 19 | 36 | } |
| 20 | 37 | |
| 21 | 38 | /** |
| 22 | | * @returns {boolean} |
| 39 | * Checks if the triangle is isosceles (at least two sides equal). |
| 40 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 41 | * Handled Edge Cases: Invalid triangle, at least two sides equal, no sides equal |
| 23 | 42 | */ |
| 24 | 43 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 44 | // Edge Case: Not a valid triangle (sides <= 0 or violate triangle inequality) |
| 45 | if (!this._isValidTriangle()) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | const [a, b, c] = this.sides; |
| 50 | |
| 51 | // Edge Case: At least two sides are equal |
| 52 | return a === b || b === c || a === c; |
| 26 | 53 | } |
| 27 | 54 | |
| 28 | 55 | /** |
| 29 | | * @returns {boolean} |
| 56 | * Checks if the triangle is scalene (all sides different). |
| 57 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 58 | * Handled Edge Cases: Invalid triangle, all sides different, some sides equal |
| 30 | 59 | */ |
| 31 | 60 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 61 | // Edge Case: Not a valid triangle (sides <= 0 or violate triangle inequality) |
| 62 | if (!this._isValidTriangle()) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | const [a, b, c] = this.sides; |
| 67 | |
| 68 | // Edge Case: All sides are different |
| 69 | return a !== b && b !== c && a !== c; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Helper method to validate if the sides form a valid triangle. |
| 74 | * @returns {boolean} True if valid triangle, false otherwise. |
| 75 | * Handled Edge Cases: Sides <= 0, triangle inequality violation |
| 76 | */ |
| 77 | _isValidTriangle() { |
| 78 | // Edge Case: Not exactly 3 sides |
| 79 | if (this.sides.length !== 3) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | const [a, b, c] = this.sides; |
| 84 | |
| 85 | // Edge Case: Any side has length <= 0 |
| 86 | if (a <= 0 || b <= 0 || c <= 0) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // Edge Case: Violation of triangle inequality (sum of any two sides must be >= third side) |
| 91 | if (a + b < c || b + c < a || a + c < b) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return true; |
| 33 | 96 | } |
| 34 | 97 | } |