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>

I think that after the dialog with the result, you should make it easy for the user to add three new values (e.g. clear the input fields). "Until the user decides to quit" probably means a button to close the browser, or possibly a dialog asking whether or not you want to input another set.

Here's my almost finished script. What do you guys think?

pritaeas, I tried to put a "close button" button in there but couldn't remember and didn't feel like reading the book and asking to input another set the way I have it with the dialog boxes was kind of useless I think from my POV. I'm thinking until the user decides to quit can just be clicking "X" to close the browser or so I hope.

Does anyone see anything wrong with my if...else statements or variables?

Thanks.

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.");
	}
	else {
		window.alert("These numbers can not form a triangle.");
	}
	
	if ((sideAsqrd == B2C2) || (sideBsqrd == A2C2) || (sideCsqrd == A2B2)) {
		window.alert("These numbers could also form a right 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 />
<br />
1.  After you have entered your numbers click the button directly below to see if they could
form a triangle.<br />
2.  If they can a dialog box will pop up informing you that it is possible. click "OK".<br />
3.  If your numbers can form a right triangle a second dialog box will appear stating that is possible. Click "OK".<br />
4.  If your numbers are not able to form a triangle a dialog will appear stating that it is not possible. Click "OK" and try again if you wish.<br />
<br />
<input type="button" value="Will these numbers form a Triangle? A Right Triangle?" 
onclick="triangle();" style="width: 350px" /><br />
<input type="reset" value="Clear Input Fields" style="width: 350px" /><br />
</p>
</form>
</body>
</html>

Ok I think I've got it finished. What do you guys think? I finally figured out how to arrange my if, else if, & else statements so that only one dialog box will appear and let you know what you need. Please look it over and give me some constructive criticism. Thanks.

Jake

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<title>Can these numbers form a triangle?</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) && (sideAsqrd == B2C2) || (sideBsqrd == A2C2) || (sideCsqrd == A2B2)) {
		window.alert("These numbers could form a right triangle.");
	}
	
	else if ((AB > C) && (AC > B) && (BC > A)) {
		window.alert("These numbers could form a triangle but not a right triangle.");
	}
	else {
		window.alert("These numbers 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 />
<br />
  After you have entered your numbers click the button directly below to see if they could
form a triangle.<br />
  A dialog box will appear and let you know whether your three numbers will be able to form a triangle or not, or 
  if you are able to form a right triangle. <br />
  Clear the input fields with the button below and try again if you like.
<br />
<input type="button" value="Will these numbers form a Triangle? A Right Triangle?" 
onclick="triangle();" style="width: 350px" /><br />
<input type="reset" value="Clear Input Fields" style="width: 350px" /><br />
</p>
</form>
</body>
</html>

Line 15-17 and 19-21, you can just do...

var sideAsqrd = A*A;
var sideBsqrd = B*B;
var sideCsqrd = C*C;

var AB = A + B;//side A + B to check if a triangle can be formed
var AC = A + C;//side A + C
var BC = B + C;//side B + C

The reason is that you have already read the value for A, B, and C in line 11-13.

The rest looks fine. The format is up to you, so no need to change. If I were you, I would just compute it before the if statement.

// in this case, I don't need variable AB, AC, and BC
var isTriangle = ((A+B)>C) && ((A+C)>B) && ((B+C)>A)  // gives me a true or false
if (isTriangle && (sideAsqrd == B2C2) || (sideBsqrd == A2C2) || (sideCsqrd == A2B2)) {
}
else if (isTriangle) {
}
else {
}

Thanks. It does make sense to work it that way. I guess especially in my case since I may not arrange things correctly until I put some thought into it. Plus it'll keep down on the text and be more readable/manageable. Thanks Taywin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.