| 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 | * Handled Edge Cases: invalid triangle, all sides equal, not all sides equal |
| 16 | 23 | */ |
| 17 | 24 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 25 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 26 | if (!this._isValidTriangle()) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | const [a, b, c] = this.sides; |
| 31 | return a === b && b === c; |
| 19 | 32 | } |
| 20 | 33 | |
| 21 | 34 | /** |
| 22 | | * @returns {boolean} |
| 35 | * Checks if the triangle is isosceles (at least two sides equal). |
| 36 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 37 | * Handled Edge Cases: invalid triangle, exactly two sides equal, all sides equal, no sides equal |
| 23 | 38 | */ |
| 24 | 39 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 40 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 41 | if (!this._isValidTriangle()) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | const [a, b, c] = this.sides; |
| 46 | return a === b || b === c || a === c; |
| 26 | 47 | } |
| 27 | 48 | |
| 28 | 49 | /** |
| 29 | | * @returns {boolean} |
| 50 | * Checks if the triangle is scalene (all sides different). |
| 51 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 52 | * Handled Edge Cases: invalid triangle, all sides different, any two sides equal |
| 30 | 53 | */ |
| 31 | 54 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 55 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 56 | if (!this._isValidTriangle()) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | const [a, b, c] = this.sides; |
| 61 | return a !== b && b !== c && a !== c; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Validates if the sides form a valid triangle. |
| 66 | * All sides must be positive and satisfy the triangle inequality. |
| 67 | * @returns {boolean} True if valid triangle, false otherwise. |
| 68 | * Handled Edge Cases: sides <= 0, triangle inequality violation |
| 69 | */ |
| 70 | _isValidTriangle() { |
| 71 | const [a, b, c] = this.sides; |
| 72 | |
| 73 | // Edge Case: Any side has length <= 0 |
| 74 | if (a <= 0 || b <= 0 || c <= 0) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Triangle inequality is violated |
| 79 | if (a + b < c || b + c < a || a + c < b) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 33 | 84 | } |
| 34 | 85 | } |