...

View Full Version : JavaScript help - My IF, Else Statements not working



JavaDan
11-11-2004, 03:37 AM
I have a simple class project i can't seem to get working. The project is as follows. "Determine if 3 numbers could equal 3 sides of a triangle". To do this i have the user enter three numbers "A, B, C". After that, I add all the combinations up and compare them to one of the sides. If all of combinations are larger than the 3rd side, it is a triangle. See my code below. Please help.

P.S I am sure there are more effiecient ways to make this code work, but i need to do it this way for class. Thanks again!





////Code starts here
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Prime Number</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
function checkForTriangle() {
num1 = parseInt(document.triangleForm.number1.value);
num2 = parseInt(document.triangleForm.number2.value);
num3 = parseInt(document.triangleForm.number3.value);
sideA = (num1 + num2);
sideB = (num1 + num3);
sideC = (num2 + num3);
if (sideA > num3)
if (sideB > num2)
if (sideC > num1)
alert("These 3 sides CAN be a triangle");
else
alert("These 3 sides CAN NOT be a triangle");
}
</script>


</head>

<body>

<form name="triangleForm" action="">
<p>Enter number 1:
<input type="text" name="number1"><br>
<p>Enter number 2:
<input type="text" name="number2"><br>
<P>Enter number 3:
<input type="text" name="number3"><br>
<input type="button" value="Triangle or Not" onClick="checkForTriangle();">
</p>
</form>

</body>
</html>

////Code Ends here

fci
11-11-2004, 03:47 AM
<script language="JavaScript" type="text/javascript">
function checkForTriangle()
{
num1 = parseInt(document.triangleForm.number1.value);
num2 = parseInt(document.triangleForm.number2.value);
num3 = parseInt(document.triangleForm.number3.value);
sideA = (num1 + num2);
sideB = (num1 + num3);
sideC = (num2 + num3);
if (sideA > num3 || sideB > num2 || sideC > num1) {
alert("These 3 sides CAN be a triangle");
} else {
alert("These 3 sides CAN NOT be a triangle");
}
}
</script>
?

JavaDan
11-12-2004, 06:20 PM
Still no go. I tried it, and now every variable i enter into the text fields comes back as "These three sides can be a triangle". Here is the whole code. Let me know if anyone can figure this out. Thanks!!!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Triangle or Not</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
function checkForTriangle()
{
num1 = parseInt(document.triangleForm.number1.value);
num2 = parseInt(document.triangleForm.number2.value);
num3 = parseInt(document.triangleForm.number3.value);
sideA = (num1 + num2);
sideB = (num1 + num3);
sideC = (num2 + num3);
if (sideA > num3 || sideB > num2 || sideC > num1) {
alert("These 3 sides CAN be a triangle");
} else {
alert("These 3 sides CAN NOT be a triangle");
}
}
</script>


</head>

<body>

<form name="triangleForm" action="">
<p>Enter number 1:
<input type="text" name="number1"><br>
<p>Enter number 2:
<input type="text" name="number2"><br>
<P>Enter number 3:
<input type="text" name="number3"><br>
<input type="button" value="Triangle or Not" onClick="checkForTriangle();">
</p>
</form>

</body>
</html>

Garadon
11-12-2004, 06:39 PM
I think it should be && not ||

so that each side is always less than the 2 other sides added. but then again this kind of math has never been me.

JavaDan
11-12-2004, 07:52 PM
Everything is working great now.. Thanks for the help!!!

-Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum