| 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: Ensure exactly three sides are provided |
| 13 | if (sides.length !== 3) { |
| 14 | this.sides = []; |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | this.sides = sides; |
| 12 | 19 | } |
| 13 | 20 | |
| 14 | 21 | /** |
| 15 | | * @returns {boolean} |
| 22 | * Checks if the triangle is equilateral (all sides equal). |
| 23 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 24 | * Handled Edge Cases: Invalid triangle, all sides equal |
| 16 | 25 | */ |
| 17 | 26 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 27 | // Edge Case: Check for valid triangle first |
| 28 | if (!this._isValidTriangle()) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | const [a, b, c] = this.sides; |
| 33 | return a === b && b === c; |
| 19 | 34 | } |
| 20 | 35 | |
| 21 | 36 | /** |
| 22 | | * @returns {boolean} |
| 37 | * Checks if the triangle is isosceles (at least two sides equal). |
| 38 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 39 | * Handled Edge Cases: Invalid triangle, at least two sides equal |
| 23 | 40 | */ |
| 24 | 41 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 42 | // Edge Case: Check for valid triangle first |
| 43 | if (!this._isValidTriangle()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | const [a, b, c] = this.sides; |
| 48 | return a === b || b === c || a === c; |
| 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: Check for valid triangle first |
| 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 | } |
| 65 | |
| 66 | /** |
| 67 | * Validates if the sides form a valid triangle. |
| 68 | * @returns {boolean} True if valid triangle, false otherwise. |
| 69 | * Handled Edge Cases: Zero or negative sides, triangle inequality violation |
| 70 | */ |
| 71 | _isValidTriangle() { |
| 72 | // Edge Case: Check if we have exactly 3 sides |
| 73 | if (this.sides.length !== 3) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | const [a, b, c] = this.sides; |
| 78 | |
| 79 | // Edge Case: Check for non-positive sides |
| 80 | if (a <= 0 || b <= 0 || c <= 0) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | // Edge Case: Check 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 | } |