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