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