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