| 5 | 5 | |
| 6 | 6 | export class Triangle { |
| 7 | 7 | /** |
| 8 | | * @param {...number} sides |
| 8 | * Creates a Triangle instance with the given sides. |
| 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: Not enough sides provided |
| 13 | if (sides.length !== 3) { |
| 14 | throw new Error('Exactly three sides must be provided'); |
| 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 or 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 | const [a, b, c] = this.sides; |
| 42 | return a === b && b === c; |
| 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 or 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 | const [a, b, c] = this.sides; |
| 62 | return a === b || b === c || a === c; |
| 26 | 63 | } |
| 27 | 64 | |
| 28 | 65 | /** |
| 29 | | * @returns {boolean} |
| 66 | * Checks if the triangle is scalene (all sides different). |
| 67 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 68 | * Handled Edge Cases: Invalid triangle, zero or negative sides, all sides different |
| 30 | 69 | */ |
| 31 | 70 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 71 | // Edge Case: Invalid triangle |
| 72 | if (!this._isValidTriangle()) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // Edge Case: All sides must be positive |
| 77 | if (this.sides.some(side => side <= 0)) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | const [a, b, c] = this.sides; |
| 82 | return a !== b && b !== c && a !== c; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Validates if the sides form a valid triangle according to triangle inequality. |
| 87 | * @returns {boolean} True if valid triangle, false otherwise. |
| 88 | * Handled Edge Cases: Triangle inequality violation |
| 89 | */ |
| 90 | _isValidTriangle() { |
| 91 | const [a, b, c] = this.sides; |
| 92 | |
| 93 | // Edge Case: Triangle inequality violation |
| 94 | return (a + b >= c) && (b + c >= a) && (a + c >= b); |
| 33 | 95 | } |
| 34 | 96 | } |