Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit e158922

Browse files
committed
is-valid-triangle.js updated
1 parent 9a62aee commit e158922

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

week-3/implement/is-valid-triangle.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,35 @@
3838
// Then it should return true because the input forms a valid triangle.
3939

4040
// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem.
41+
//....................................................................
42+
//Answer
43+
44+
// we start with a function to check if three numbers can form a triangle
45+
function isValidTriangle(a, b, c) {
46+
// here we are saying that sides should be greater than zero
47+
if (a <= 0 || b <= 0 || c <= 0) {
48+
return false;
49+
}
50+
51+
// then we need to check if the sum of any two sides is greater than the length of the third side for all possible combinations of sides
52+
if (a + b > c && a + c > b && b + c > a) {
53+
return true; // this means it is a valid triangle
54+
} else {
55+
return false; // It's not a valid triangle
56+
}
57+
}
58+
59+
// Test examples
60+
61+
// Test for (Invalid )Triangle
62+
const testInvalidTriangle = isValidTriangle(2, 3, 6);
63+
console.assert(testInvalidTriangle === false);
64+
65+
// Test for Invalid Input: A side is less than or equal to zero
66+
const testInvalidInput = isValidTriangle(0, 4, 5);
67+
console.assert(testInvalidInput === false);
68+
69+
// Test for a Valid Triangle
70+
const testValidTriangle = isValidTriangle(3, 4, 5);
71+
console.assert(testValidTriangle === true);
72+

0 commit comments

Comments
 (0)