| 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 valid according to triangle inequality rules. |
| 27 | * A valid triangle has all sides > 0 and sum of any two sides >= third side. |
| 28 | * @returns {boolean} True if the triangle is valid, false otherwise. |
| 29 | */ |
| 30 | isValidTriangle() { |
| 31 | const [a, b, c] = this.sides; |
| 32 | |
| 33 | // Edge Case: Any side has length <= 0 |
| 34 | if (a <= 0 || b <= 0 || c <= 0) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // Edge Case: Triangle inequality violation |
| 39 | if (a + b < c || b + c < a || a + c < b) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Determines if the triangle is equilateral (all sides equal). |
| 48 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 16 | 49 | */ |
| 17 | 50 | get isEquilateral() { |
| 18 | | 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 | const [a, b, c] = this.sides; |
| 57 | return a === b && b === c; |
| 19 | 58 | } |
| 20 | 59 | |
| 21 | 60 | /** |
| 22 | | * @returns {boolean} |
| 61 | * Determines if the triangle is isosceles (at least two sides equal). |
| 62 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 23 | 63 | */ |
| 24 | 64 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 65 | // Edge Case: Invalid triangle |
| 66 | if (!this.isValidTriangle()) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | const [a, b, c] = this.sides; |
| 71 | return a === b || b === c || a === c; |
| 26 | 72 | } |
| 27 | 73 | |
| 28 | 74 | /** |
| 29 | | * @returns {boolean} |
| 75 | * Determines if the triangle is scalene (all sides different). |
| 76 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 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 | return a !== b && b !== c && a !== c; |
| 33 | 86 | } |
| 34 | 87 | } |
| 88 | |
| 89 | // Handled Edge Cases: Invalid number of sides, non-numeric side lengths, sides <= 0, triangle inequality violation |
| 90 | // Handled Edge Cases: Invalid triangle, all sides equal |
| 91 | // Handled Edge Cases: Invalid triangle, at least two sides equal |
| 92 | // Handled Edge Cases: Invalid triangle, all sides different |