first time posting here so sorry if this isn’t as clear or efficient as can be, but I’m trying to write a program to take user inputs for 3 sides of a triangle and see if they are a valid triangle based on these rules:
a + b > c
a + c > b
b + c > a
So far, I’m just trying to prove a + b > c (which I called condition1). But in trying to solve this, it seems that the if statement I have is ignoring the a + b part and just checking a > c. Can anyone tell me what I’m doing wrong?
function isTriangle() {
var input1 = document.getElementById("a"); // fetch the 3 numbers
var input2 = document.getElementById("b");
var input3 = document.getElementById("c");
var displayAnswer = document.getElementById("display");
function condition1(){
if ((input1.value + input2.value) > input3.value)
{
return true;
}
else
{
return false;
}
}
//our goal here is to use triangle inequality theorem
if (condition1(true)) {
displayAnswer.innerHTML = "These three sides can form a right triangle!";
}
else {
displayAnswer.innerHTML = "These three sides cannot form a right triangle!";
}
}
<!DOCTYPE html>
<html>
<head>
<title>Project 3</title>
<script src="proj3.js" type="text/javascript"></script>
</head>
<body>
<h1>Triangle Tester</h1>
<p> Please input the length (integer) of the first side of the triangle: </p>
<input id="a" type="text" size="3" />
<p> Please input the length (integer) of the second side of the triangle: </p>
<input id="b" type="text" size="3" />
<p> Please input the length (integer) of the third side of the triangle: </p>
<input id="c" type="text" size="3" />
<br />
<br />
<span id="display"></span>
<br />
<button onclick="isTriangle();">(2)</button>
</body>
</html>