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