| 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 (not exactly 3) |
| 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, non-positive sides, inequality violation, all sides equal |
| 16 | 25 | */ |
| 17 | 26 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 27 | // Edge Case: Not enough sides to form a triangle |
| 28 | if (this.sides.length !== 3) return false; |
| 29 | |
| 30 | const [a, b, c] = this.sides; |
| 31 | |
| 32 | // Edge Case: Sides with zero or negative length |
| 33 | if (a <= 0 || b <= 0 || c <= 0) return false; |
| 34 | |
| 35 | // Edge Case: Triangle inequality violation |
| 36 | if (a + b < c || b + c < a || a + c < b) return false; |
| 37 | |
| 38 | // Edge Case: All sides are equal (equilateral condition) |
| 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, non-positive sides, inequality violation, at least two sides equal |
| 23 | 46 | */ |
| 24 | 47 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 48 | // Edge Case: Not enough sides to form a triangle |
| 49 | if (this.sides.length !== 3) return false; |
| 50 | |
| 51 | const [a, b, c] = this.sides; |
| 52 | |
| 53 | // Edge Case: Sides with zero or negative length |
| 54 | if (a <= 0 || b <= 0 || c <= 0) return false; |
| 55 | |
| 56 | // Edge Case: Triangle inequality violation |
| 57 | if (a + b < c || b + c < a || a + c < b) return false; |
| 58 | |
| 59 | // Edge Case: At least two sides are equal (isosceles condition) |
| 60 | return a === b || b === c || a === c; |
| 26 | 61 | } |
| 27 | 62 | |
| 28 | 63 | /** |
| 29 | | * @returns {boolean} |
| 64 | * Checks if the triangle is scalene (all sides different). |
| 65 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 66 | * Handled Edge Cases: Invalid triangle, non-positive sides, inequality violation, all sides different |
| 30 | 67 | */ |
| 31 | 68 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 69 | // Edge Case: Not enough sides to form a triangle |
| 70 | if (this.sides.length !== 3) return false; |
| 71 | |
| 72 | const [a, b, c] = this.sides; |
| 73 | |
| 74 | // Edge Case: Sides with zero or negative length |
| 75 | if (a <= 0 || b <= 0 || c <= 0) return false; |
| 76 | |
| 77 | // Edge Case: Triangle inequality violation |
| 78 | if (a + b < c || b + c < a || a + c < b) return false; |
| 79 | |
| 80 | // Edge Case: All sides are different (scalene condition) |
| 81 | return a !== b && b !== c && a !== c; |
| 33 | 82 | } |
| 34 | 83 | } |