| 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 | throw new Error('A triangle must have exactly 3 sides'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Non-numeric side lengths |
| 18 | if (sides.some(side => typeof side !== 'number')) { |
| 19 | throw new Error('All sides must be numbers'); |
| 20 | } |
| 21 | |
| 22 | this.sides = sides; |
| 12 | 23 | } |
| 13 | 24 | |
| 14 | 25 | /** |
| 15 | | * @returns {boolean} |
| 26 | * Checks if the triangle is equilateral (all sides equal). |
| 27 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 28 | * Handled Edge Cases: Invalid triangle (sides <= 0 or violate triangle inequality), all sides equal |
| 16 | 29 | */ |
| 17 | 30 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 31 | const [a, b, c] = this.sides; |
| 32 | |
| 33 | // Edge Case: Invalid triangle (sides <= 0) |
| 34 | if (a <= 0 || b <= 0 || c <= 0) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // Edge Case: Violates triangle inequality |
| 39 | if (a + b < c || b + c < a || a + c < b) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // Edge Case: All sides equal |
| 44 | return a === b && b === c; |
| 19 | 45 | } |
| 20 | 46 | |
| 21 | 47 | /** |
| 22 | | * @returns {boolean} |
| 48 | * Checks if the triangle is isosceles (at least two sides equal). |
| 49 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 50 | * Handled Edge Cases: Invalid triangle (sides <= 0 or violate triangle inequality), at least two sides equal |
| 23 | 51 | */ |
| 24 | 52 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 53 | const [a, b, c] = this.sides; |
| 54 | |
| 55 | // Edge Case: Invalid triangle (sides <= 0) |
| 56 | if (a <= 0 || b <= 0 || c <= 0) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | // Edge Case: Violates triangle inequality |
| 61 | if (a + b < c || b + c < a || a + c < b) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // Edge Case: At least two sides equal |
| 66 | return a === b || b === c || a === c; |
| 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 <= 0) |
| 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 different |
| 88 | return a !== b && b !== c && a !== c; |
| 33 | 89 | } |
| 34 | 90 | } |