| 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 sides form a valid triangle. |
| 27 | * A valid triangle has all sides with length > 0 and satisfies the triangle inequality. |
| 28 | * @returns {boolean} True if the sides form a valid triangle, false otherwise. |
| 29 | * Handled Edge Cases: sides with zero or negative length, sides that violate triangle inequality |
| 30 | */ |
| 31 | isValidTriangle() { |
| 32 | const [a, b, c] = this.sides; |
| 33 | |
| 34 | // Edge Case: Side with zero or negative length |
| 35 | if (a <= 0 || b <= 0 || c <= 0) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | // Edge Case: Violation of triangle inequality |
| 40 | if (a + b < c || b + c < a || a + c < b) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Checks if the triangle is equilateral (all sides equal). |
| 49 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 50 | * Handled Edge Cases: invalid triangles |
| 16 | 51 | */ |
| 17 | 52 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 53 | // Edge Case: Invalid triangle |
| 54 | if (!this.isValidTriangle()) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | const [a, b, c] = this.sides; |
| 59 | return a === b && b === c; |
| 19 | 60 | } |
| 20 | 61 | |
| 21 | 62 | /** |
| 22 | | * @returns {boolean} |
| 63 | * Checks if the triangle is isosceles (at least two sides equal). |
| 64 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 65 | * Handled Edge Cases: invalid triangles |
| 23 | 66 | */ |
| 24 | 67 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 68 | // Edge Case: Invalid triangle |
| 69 | if (!this.isValidTriangle()) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | const [a, b, c] = this.sides; |
| 74 | return a === b || b === c || a === c; |
| 26 | 75 | } |
| 27 | 76 | |
| 28 | 77 | /** |
| 29 | | * @returns {boolean} |
| 78 | * Checks if the triangle is scalene (all sides different). |
| 79 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 80 | * Handled Edge Cases: invalid triangles |
| 30 | 81 | */ |
| 31 | 82 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 83 | // Edge Case: Invalid triangle |
| 84 | if (!this.isValidTriangle()) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | const [a, b, c] = this.sides; |
| 89 | return a !== b && b !== c && a !== c; |
| 33 | 90 | } |
| 34 | 91 | } |