Hello, I'm new to Javascript and I'm trying to write a code that when a user enters three numbers will tell if the numbers can form a triangle or not, or if the triangle is a right triangle. I can't see why the code won't work but I bet someone in here can pinpoint the problem for me. Thank you!


Here's the code:

<!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);
sideASquared = sideA * sideA;
sideBSquared = sideB * sideB;
sideCSquared = sideC * sideC;
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");
}

if sideASquared == (sideBSquared + sideCSquared) ||
   sideBSquared == (sideASquared + sideCSquared) ||
   sideCSquared == (sideASquared + sideBSquared) || {
alert("This is a Right 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="Is this a Triangle?" onClick="checkForTriangle();">
</p>
</form>

</body>
</html>

The 1st problem goes with your script tags! Let's do some changes!
Instead of--> <script language="JavaScript" type="text/javascript"> Simply do it like this!

<script type="text/javascript">
<!--

//Your codes goes here!

//-->

</script>
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");
} 

/*
Code Break goes here!
Try to recall this line of your code. I'll make some changes and hope this can solve the issue */ 


if (( sideASquared == (sideBSquared + sideCSquared)) ||
( sideBSquared == (sideASquared + sideCSquared)) ||
( sideCSquared == (sideASquared + sideBSquared)))
{
alert("This is a Right Triangle");
}

Thank you Essential. It works for testing triangle but won't work for Right Triangle. Do I need to create a new function for the right triangle?

Tested and working good!
I add up some code chunks to validate your fields'! Enjoy coding...

<!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 type="text/javascript">
var validValue = /\d+$/;
function checkForTriangle()
{
var alertMsg = '\nOnly numbers\' are allowed on this field!';

for ( var x = 0; x <= 2; x++ ) 
{ 
 alertMsg = ( document.triangleForm.elements[x].value == '' ) ? '\nYou must complete all entries on this field!'  : alertMsg;
 if ( !validValue.test( document.triangleForm.elements[x].value )) { alert( alertMsg ); return false; }
 }  
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 );
sideASquared = sideA * sideA;
sideBSquared = sideB * sideB;
sideCSquared = sideC * sideC;
var rTa = sideBSquared + sideCSquared;
var rTb = sideASquared + sideCSquared; 
var rTc = sideASquared + sideBSquared; 

if ( sideA > num3 && sideB > num2 && sideC > num1 ) {
alert("These 3 sides CAN be a triangle");
} 
else if (( sideASquared == rTa ) || 
( sideBSquared == rTb ) || 
( sideCSquared == rTc )) {
alert("This is a Right Triangle");
} else {
alert("These 3 sides CAN NOT be a triangle");
}
}
</script>
</head>
<body>
<form name="triangleForm"action="#" onsubmit="return false;">
<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="Is this a Triangle?" onClick="checkForTriangle();">
</p>
</form>
</body>
</html>

! I got it to test now. Thank you very much

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.