| 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 values |
| 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 | const [a, b, c] = this.sides; |
| 37 | |
| 38 | // Edge Case: Zero or negative sides |
| 39 | if (a <= 0 || b <= 0 || c <= 0) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // Edge Case: All sides equal |
| 44 | return a === b && b === c; |
| 45 | |
| 46 | // Handled Edge Cases: Invalid triangle, zero/negative sides, all sides equal |
| 19 | 47 | } |
| 20 | 48 | |
| 21 | 49 | /** |
| 22 | | * @returns {boolean} |
| 50 | * Checks if the triangle is isosceles (at least two sides equal). |
| 51 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 52 | * Handled Edge Cases: Invalid triangle, zero/negative sides, at least two sides equal |
| 23 | 53 | */ |
| 24 | 54 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 55 | // Edge Case: Invalid triangle |
| 56 | if (!this._isValidTriangle()) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | const [a, b, c] = this.sides; |
| 61 | |
| 62 | // Edge Case: Zero or negative sides |
| 63 | if (a <= 0 || b <= 0 || c <= 0) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // Edge Case: At least two sides equal |
| 68 | return a === b || b === c || a === c; |
| 69 | |
| 70 | // Handled Edge Cases: Invalid triangle, zero/negative sides, at least two sides equal |
| 26 | 71 | } |
| 27 | 72 | |
| 28 | 73 | /** |
| 29 | | * @returns {boolean} |
| 74 | * Checks if the triangle is scalene (all sides different). |
| 75 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 76 | * Handled Edge Cases: Invalid triangle, zero/negative sides, all sides different |
| 30 | 77 | */ |
| 31 | 78 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 79 | // Edge Case: Invalid triangle |
| 80 | if (!this._isValidTriangle()) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | const [a, b, c] = this.sides; |
| 85 | |
| 86 | // Edge Case: Zero or negative sides |
| 87 | if (a <= 0 || b <= 0 || c <= 0) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | // Edge Case: All sides different |
| 92 | return a !== b && b !== c && a !== c; |
| 93 | |
| 94 | // Handled Edge Cases: Invalid triangle, zero/negative sides, all sides different |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Helper method to check if the sides form a valid triangle. |
| 99 | * @returns {boolean} True if the sides form a valid triangle, false otherwise. |
| 100 | * Handled Edge Cases: Triangle inequality, zero/negative sides |
| 101 | */ |
| 102 | _isValidTriangle() { |
| 103 | const [a, b, c] = this.sides; |
| 104 | |
| 105 | // Edge Case: Zero or negative sides |
| 106 | if (a <= 0 || b <= 0 || c <= 0) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // Edge Case: Triangle inequality |
| 111 | return (a + b >= c) && (b + c >= a) && (a + c >= b); |
| 112 | |
| 113 | // Handled Edge Cases: Triangle inequality, zero/negative sides |
| 33 | 114 | } |
| 34 | 115 | } |