| 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 three sides of the triangle. |
| 9 | 5 | */ |
| 10 | 6 | constructor(...sides) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 7 | if (sides.length !== 3) { |
| 8 | throw new Error('A triangle must have exactly 3 sides'); |
| 9 | } |
| 10 | this.sides = sides; |
| 12 | 11 | } |
| 13 | 12 | |
| 14 | 13 | /** |
| 15 | | * @returns {boolean} |
| 14 | * Checks if the triangle is equilateral (all sides equal). |
| 15 | * @returns {boolean} True if the triangle is equilateral, false otherwise. |
| 16 | * Handled Edge Cases: non-triangle inputs, sides with zero or negative values, invalid triangle inequality |
| 16 | 17 | */ |
| 17 | 18 | get isEquilateral() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 19 | // Edge Case: Check if it's a valid triangle first |
| 20 | if (!this._isValidTriangle()) { |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | const [a, b, c] = this.sides; |
| 25 | return a === b && b === c; |
| 19 | 26 | } |
| 20 | 27 | |
| 21 | 28 | /** |
| 22 | | * @returns {boolean} |
| 29 | * Checks if the triangle is isosceles (at least two sides equal). |
| 30 | * @returns {boolean} True if the triangle is isosceles, false otherwise. |
| 31 | * Handled Edge Cases: non-triangle inputs, sides with zero or negative values, invalid triangle inequality |
| 23 | 32 | */ |
| 24 | 33 | get isIsosceles() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 34 | // Edge Case: Check if it's a valid triangle first |
| 35 | if (!this._isValidTriangle()) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | const [a, b, c] = this.sides; |
| 40 | return a === b || b === c || a === c; |
| 26 | 41 | } |
| 27 | 42 | |
| 28 | 43 | /** |
| 29 | | * @returns {boolean} |
| 44 | * Checks if the triangle is scalene (all sides different). |
| 45 | * @returns {boolean} True if the triangle is scalene, false otherwise. |
| 46 | * Handled Edge Cases: non-triangle inputs, sides with zero or negative values, invalid triangle inequality |
| 30 | 47 | */ |
| 31 | 48 | get isScalene() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 49 | // Edge Case: Check if it's a valid triangle first |
| 50 | if (!this._isValidTriangle()) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | const [a, b, c] = this.sides; |
| 55 | return a !== b && b !== c && a !== c; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Validates if the sides form a valid triangle. |
| 60 | * @returns {boolean} True if valid triangle, false otherwise. |
| 61 | * Handled Edge Cases: sides with zero or negative values, invalid triangle inequality |
| 62 | */ |
| 63 | _isValidTriangle() { |
| 64 | const [a, b, c] = this.sides; |
| 65 | |
| 66 | // Edge Case: All sides must have positive length |
| 67 | if (a <= 0 || b <= 0 || c <= 0) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Edge Case: Triangle inequality must hold |
| 72 | if (a + b < c || b + c < a || a + c < b) { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 33 | 77 | } |
| 34 | 78 | } |