| 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 three sides of the triangle. |
| 9 | 10 | */ |
| 10 | 11 | constructor(...sides) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Edge Case: Incorrect number of sides provided |
| 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 triangles (sides <= 0, triangle inequality violation) |
| 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 must be > 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 a === b && b === c; |
| 19 | 44 | } |
| 20 | 45 | |
| 21 | 46 | /** |
| 22 | | * @returns {boolean} |
| 47 | * Checks if the triangle is isosceles (at least two sides equal). |
| 48 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 49 | * Handled Edge Cases: Invalid triangles (sides <= 0, triangle inequality violation) |
| 23 | 50 | */ |
| 24 | 51 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 52 | const [a, b, c] = this.sides; |
| 53 | |
| 54 | // Edge Case: Invalid triangle (sides must be > 0) |
| 55 | if (a <= 0 || b <= 0 || c <= 0) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // Edge Case: Triangle inequality violation |
| 60 | if (a + b < c || b + c < a || a + c < b) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | return a === b || b === c || a === c; |
| 26 | 65 | } |
| 27 | 66 | |
| 28 | 67 | /** |
| 29 | | * @returns {boolean} |
| 68 | * Checks if the triangle is scalene (all sides different). |
| 69 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 70 | * Handled Edge Cases: Invalid triangles (sides <= 0, triangle inequality violation) |
| 30 | 71 | */ |
| 31 | 72 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 73 | const [a, b, c] = this.sides; |
| 74 | |
| 75 | // Edge Case: Invalid triangle (sides must be > 0) |
| 76 | if (a <= 0 || b <= 0 || c <= 0) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Edge Case: Triangle inequality violation |
| 81 | if (a + b < c || b + c < a || a + c < b) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return a !== b && b !== c && a !== c; |
| 33 | 86 | } |
| 34 | 87 | } |