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