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