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