Hey everyone. Need some assistance again please. Here is what my assignment says word for word:
The sum of the lengths of any two sides of a triangle must be greater than the length of the third side. For example, the numbers 3, 4, & 5 can form a triangle because 3+4>5, 4+5>3, and 5+3>4. In contrast the numbers 1, 2, & 5 cannot form a triangle because 1+2<5. Thus if you are given any three integers, you can determine whether they could possibly form a triangle or not by applying this general principle.
Write a JavaScript program that allows a user to input three integers using text boxes in a form. (Hint: You need to use the built-in parseInt function to convert the input strings to integers.) Test the three integers to determine if they can be formed into a triangle using the given rule above. Also test if the resulting triangle would be a right triangle using the Pythagorean theorem, namely that the square of the hypotenuse(the longest side) equals the sum of squares of the other two sides. Display an alert box to inform the user whether their integers can form a triangle. Continue accepting set of three integers and testing them until the user decides to quit.
For the instructions above...I haven't any idea what that lines mean exactly. Any thoughts?
Below is what I have so far.
I think I'm off to a good start. I think my variables are good and complete. I do need to work on the function though and the if statements. Which is a bit confusing for me. So please throw out any suggestions to the function that I may need to make. Please just let me know what you think in "text". I don't exactly want it to be done for me so that hopefully I'll pick it up a bit better if that makes since. Thanks for any suggestions.
Jake
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function triangle() {
var A = parseInt(document.sides.A.value);//converts input for side A to integer
var B = parseInt(document.sides.B.value);//converts for side B
var C = parseInt(document.sides.C.value);//converts for side C
var sideAsqrd = parseInt(document.sides.A.value) * parseInt(document.sides.A.value);//side A squared for pythagorean theorem
var sideBsqrd = parseInt(document.sides.B.value) * parseInt(document.sides.B.value);//side B squared
var sideCsqrd = parseInt(document.sides.C.value) * parseInt(document.sides.C.value);//side C squared
var AB = parseInt(document.sides.A.value) + parseInt(document.sides.B.value);//side A + B to check if a triangle can be formed
var AC = parseInt(document.sides.A.value) + parseInt(document.sides.C.value);//side A + C
var BC = parseInt(document.sides.B.value) + parseInt(document.sides.C.value);//side B + C
var A2B2 = sideAsqrd + sideBsqrd;
var A2C2 = sideAsqrd + sideCsqrd;
var B2C2 = sideBsqrd + sideCsqrd;
if ((AB > C) && (AC > B) && (BC > A)) {
window.alert("These numbers could form a triangle.");
}
if ((sideAsqrd == B2C2) || (sideBsqrd == A2C2) || (sideCsqrd == A2B2)) {
window.alert("These numbers could also form a right triangle.");
}
else {
window.alert("These 3 sides can not form a triangle.");
}
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS-->
</script>
</head>
<body>
<p>Please enter your numbers below to see if they could form a triangle.</p>
<form action="" name="sides">
<p>Enter side A:
<input type="text" name="A" ><br />
<p>Enter side B:
<input type="text" name="B" ><br />
<P>Enter side C:
<input type="text" name="C" ><br />
<input type="button" value="Triangle?"
onclick="triangle();">
</p>
</form>
</body>
</html>