chris

Finished
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
61export class Triangle {
72 /**
8 * @param {...number} sides
3 * Creates a Triangle instance with the given side lengths.
4 * @param {...number} sides - The lengths of the sides of the triangle.
5 * @throws {Error} If the number of sides is not exactly three.
96 */
107 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;
1212 }
1313
1414 /**
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 with all sides equal
20 * - Triangle with all sides equal but zero or negative (invalid triangle)
1621 */
1722 get isEquilateral() {
18 throw new Error('Remove this line and implement the function');
23 // Edge Case: Invalid triangle (sides <= 0) or violates triangle inequality
24 if (!this._isValidTriangle()) {
25 return false;
26 }
27
28 const [a, b, c] = this.sides;
29 return a === b && b === c;
1930 }
2031
2132 /**
22 * @returns {boolean}
33 * Checks if the triangle is isosceles (at least two sides equal).
34 * @returns {boolean} True if the triangle is isosceles, false otherwise.
35 *
36 * Handled Edge Cases:
37 * - Triangle with exactly two sides equal
38 * - Triangle with all sides equal (also isosceles)
39 * - Triangle with two equal sides but zero or negative sides (invalid triangle)
2340 */
2441 get isIsosceles() {
25 throw new Error('Remove this line and implement the function');
42 // Edge Case: Invalid triangle (sides <= 0) or violates triangle inequality
43 if (!this._isValidTriangle()) {
44 return false;
45 }
46
47 const [a, b, c] = this.sides;
48 return a === b || b === c || a === c;
2649 }
2750
2851 /**
29 * @returns {boolean}
52 * Checks if the triangle is scalene (all sides different).
53 * @returns {boolean} True if the triangle is scalene, false otherwise.
54 *
55 * Handled Edge Cases:
56 * - Triangle with all sides different
57 * - Triangle with all sides different but zero or negative sides (invalid triangle)
3058 */
3159 get isScalene() {
32 throw new Error('Remove this line and implement the function');
60 // Edge Case: Invalid triangle (sides <= 0) or violates triangle inequality
61 if (!this._isValidTriangle()) {
62 return false;
63 }
64
65 const [a, b, c] = this.sides;
66 return a !== b && b !== c && a !== c;
67 }
68
69 /**
70 * Validates if the sides form a valid triangle.
71 * All sides must be positive and satisfy the triangle inequality.
72 * @returns {boolean} True if valid triangle, false otherwise.
73 *
74 * Handled Edge Cases:
75 * - Sides with zero or negative values
76 * - Sides that violate triangle inequality (sum of any two sides must be >= third side)
77 */
78 _isValidTriangle() {
79 const [a, b, c] = this.sides;
80
81 // Edge Case: Sides with zero or negative values
82 if (a <= 0 || b <= 0 || c <= 0) {
83 return false;
84 }
85
86 // Edge Case: Sides that violate triangle inequality
87 if (a + b < c || b + c < a || a + c < b) {
88 return false;
89 }
90
91 return true;
3392 }
3493}
Test NameStatus
all sides are equal
Pass
any side is unequal
Pass
no sides are equal
Pass
all zero sides is not a triangle
Pass
sides may be floats
Pass
last two sides are equal
Pass
first two sides are equal
Pass
first and last sides are equal
Pass
equilateral triangles are also isosceles
Pass
no sides are equal
Pass
first triangle inequality violation
Pass
second triangle inequality violation
Pass
third triangle inequality violation
Pass
sides may be floats
Pass
no sides are equal
Pass
all sides are equal
Pass
first and second sides are equal
Pass
first and third sides are equal
Pass
second and third sides are equal
Pass
may not violate triangle inequality
Pass
sides may be floats
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.