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