This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathis-valid-triangle.js
More file actions
61 lines (54 loc) · 2.71 KB
/
is-valid-triangle.js
File metadata and controls
61 lines (54 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Implement a function isValidTriangle
function isValidTriangle(a, b, c) {
if (a <= 0 && b <= 0 && c <= 0) {
return false;
} else {
if (a + b > c && b + c > a && c + a > b) {
return true;
} else {
return false;
}
}
}
// 🗝️ Terms
// the Triangle Inequality says: the sum of any two sides is always greater than the third side.
// practical examples:
// Side a = 3
// Side b = 3
// Side c = 3
// This is a valid triangle, because a plus b = 6 and 6 is greater than 3
// Another way to write this is a + b > c
// It's also true that b + c > a
// It's also true that a + c > b
// In our function isValidTriangle, we need to return false for any triangle where the sum of any two sides is less than or equal to the length of the third side.
// and we need to return true for any triangle where the sum of any two sides is greater than the length of the third side.
// Acceptance criteria:
// Given the lengths of three sides of a triangle (a, b, c),
// When the function isValidTriangle is called with these side lengths as input,
// Then it should:
// scenario: invalid triangle
// Given the side lengths a, b, and c,
// When the sum of any two side lengths is less than or equal to the length of the third side (i.e., a + b <= c, a + c <= b, b + c <= a),
// Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side.
console.assert(
isValidTriangle(3, 3, 6) === false && isValidTriangle(2, 3, 6) === false,
"When the sum of any two side lengths is less than or equal to the length of the third side"
);
// scenario: invalid triangle
// Check for Valid Input:
// Given the sides a, b, and c,
// When any of the sides are less than or equal to zero,
// Then it should return false because a triangle cannot have zero or negative side lengths.
console.assert(
isValidTriangle(3, 3, -3) === false && isValidTriangle(1, 1, 0) === false,
"When any of the sides are less than or equal to zero"
);
// scenario: valid triangle
// Given valid side lengths where the sum of any two sides is greater than the third side,
// When the function is called with these values as input,
// Then it should return true because the input forms a valid triangle.
console.assert(
isValidTriangle(3, 3, 3) === true && isValidTriangle(3, 4, 5) === true,
"When valid side lengths where the sum of any two sides is greater than the third side"
);
// 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.