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