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