| 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 |
| 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, zero/negative sides, all sides equal |
| 16 | 29 | */ |
| 17 | 30 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 31 | // Edge Case: Invalid triangle |
| 32 | if (!this._isValidTriangle()) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | // Edge Case: All sides must be positive |
| 37 | if (this.sides.some(side => side <= 0)) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // Edge Case: All sides equal |
| 42 | return this.sides[0] === this.sides[1] && this.sides[1] === this.sides[2]; |
| 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, zero/negative sides, at least two sides equal |
| 23 | 49 | */ |
| 24 | 50 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 51 | // Edge Case: Invalid triangle |
| 52 | if (!this._isValidTriangle()) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Edge Case: All sides must be positive |
| 57 | if (this.sides.some(side => side <= 0)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Edge Case: At least two sides equal |
| 62 | return ( |
| 63 | this.sides[0] === this.sides[1] || |
| 64 | this.sides[1] === this.sides[2] || |
| 65 | this.sides[0] === this.sides[2] |
| 66 | ); |
| 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, zero/negative sides, all sides different |
| 30 | 73 | */ |
| 31 | 74 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 75 | // Edge Case: Invalid triangle |
| 76 | if (!this._isValidTriangle()) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Edge Case: All sides must be positive |
| 81 | if (this.sides.some(side => side <= 0)) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | // Edge Case: All sides different |
| 86 | return ( |
| 87 | this.sides[0] !== this.sides[1] && |
| 88 | this.sides[1] !== this.sides[2] && |
| 89 | this.sides[0] !== this.sides[2] |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Helper method to check if the sides form a valid triangle. |
| 95 | * @returns {boolean} True if the sides form a valid triangle, false otherwise. |
| 96 | * Handled Edge Cases: Triangle inequality violation |
| 97 | */ |
| 98 | _isValidTriangle() { |
| 99 | const [a, b, c] = this.sides; |
| 100 | |
| 101 | // Edge Case: Triangle inequality violation |
| 102 | return ( |
| 103 | a + b >= c && |
| 104 | b + c >= a && |
| 105 | a + c >= b |
| 106 | ); |
| 33 | 107 | } |
| 34 | 108 | } |