| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'Triangle' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | 1 | export class Triangle { |
| 7 | 2 | /** |
| 8 | | * @param {...number} sides |
| 3 | * Creates a Triangle instance with the given side lengths. |
| 4 | * @param {...number} sides - The lengths of the triangle's sides. |
| 5 | * @throws {Error} If the number of sides is not exactly three. |
| 9 | 6 | */ |
| 10 | 7 | constructor(...sides) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 8 | if (sides.length !== 3) { |
| 9 | throw new Error('A triangle must have exactly three sides'); |
| 10 | } |
| 11 | this.sides = sides; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | | * @returns {boolean} |
| 15 | * Checks if the triangle is equilateral (all sides equal). |
| 16 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 17 | * Handled Edge Cases: invalid triangle, all sides equal |
| 16 | 18 | */ |
| 17 | 19 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 20 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 21 | if (!this._isValidTriangle()) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | const [a, b, c] = this.sides; |
| 26 | return a === b && b === c; |
| 27 | |
| 28 | // Handled Edge Cases: invalid triangle, all sides equal |
| 19 | 29 | } |
| 20 | 30 | |
| 21 | 31 | /** |
| 22 | | * @returns {boolean} |
| 32 | * Checks if the triangle is isosceles (at least two sides equal). |
| 33 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 34 | * Handled Edge Cases: invalid triangle, at least two sides equal |
| 23 | 35 | */ |
| 24 | 36 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 37 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 38 | if (!this._isValidTriangle()) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | const [a, b, c] = this.sides; |
| 43 | return a === b || b === c || a === c; |
| 44 | |
| 45 | // Handled Edge Cases: invalid triangle, at least two sides equal |
| 26 | 46 | } |
| 27 | 47 | |
| 28 | 48 | /** |
| 29 | | * @returns {boolean} |
| 49 | * Checks if the triangle is scalene (all sides different). |
| 50 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 51 | * Handled Edge Cases: invalid triangle, all sides different |
| 30 | 52 | */ |
| 31 | 53 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 54 | // Edge Case: Invalid triangle (sides <= 0 or violate triangle inequality) |
| 55 | if (!this._isValidTriangle()) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | const [a, b, c] = this.sides; |
| 60 | return a !== b && b !== c && a !== c; |
| 61 | |
| 62 | // Handled Edge Cases: invalid triangle, all sides different |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Validates if the sides form a valid triangle. |
| 67 | * @private |
| 68 | * @returns {boolean} True if valid triangle, false otherwise. |
| 69 | * Handled Edge Cases: zero or negative sides, triangle inequality violation |
| 70 | */ |
| 71 | _isValidTriangle() { |
| 72 | const [a, b, c] = this.sides; |
| 73 | |
| 74 | // Edge Case: Sides with zero or negative length |
| 75 | if (a <= 0 || b <= 0 || c <= 0) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Edge Case: Violation of triangle inequality (sum of any two sides must be >= third side) |
| 80 | if (a + b < c || b + c < a || a + c < b) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | return true; |
| 85 | |
| 86 | // Handled Edge Cases: zero or negative sides, triangle inequality violation |
| 33 | 87 | } |
| 34 | 88 | } |