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