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