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