Question #24220

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, and 5 can form a triangle because 3+4 > 5, 4+5 > 3, and 5+3 > 4. In contrast, the numbers 1, 2, and 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 rule given 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 or a right

Expert's answer

<!DOCTYPE html>

<head>

<title> Tric calc </title>

<script type="text/javascript">

function calculate(){

first=parseInt(document.myForm.firstSide.value);

second=parseInt(document.myForm.secondSide.value);

third=parseInt(document.myForm.thirdSide.value);

if (first>=second+third || second>=first+third || third>=first+second){

alert('It is impossible to create a tric');

} else{

alert('It is possible to create a tric');

if (sqr(first)==sqr(second)+sqr(third) || sqr(second)==sqr(first)+sqr(third) || sqr(third)==sqr(first)+sqr(second)){

alert('tric is right');

}

}

return(0);

}

function sqr(a){

return a*a;

}

</script>

</head>

<body>

<form name="myForm">

<table>

<tr>

<td>

Input first side:

</td>

<td>

<input type="textbox" name="firstSide" value="0">

<td>

</tr>

<tr>

<td>

Input second side:

</td>

<td>

<input type="textbox" name="secondSide" value="0">

<td>

</tr>

<tr>

<td>

Input third side:

</td>

<td>

<input type="textbox" name="thirdSide" value="0">

<td>

</tr>

<tr><td colspan="2"><input type="button" name="submitButton" value="Triangle calculation" onclick="calculate()"></td></tr>

</table>

<p id='possible'></p>

<p id='rightTriangle'></p>

</form>

</body>

</html>

<head>

<title> Tric calc </title>

<script type="text/javascript">

function calculate(){

first=parseInt(document.myForm.firstSide.value);

second=parseInt(document.myForm.secondSide.value);

third=parseInt(document.myForm.thirdSide.value);

if (first>=second+third || second>=first+third || third>=first+second){

alert('It is impossible to create a tric');

} else{

alert('It is possible to create a tric');

if (sqr(first)==sqr(second)+sqr(third) || sqr(second)==sqr(first)+sqr(third) || sqr(third)==sqr(first)+sqr(second)){

alert('tric is right');

}

}

return(0);

}

function sqr(a){

return a*a;

}

</script>

</head>

<body>

<form name="myForm">

<table>

<tr>

<td>

Input first side:

</td>

<td>

<input type="textbox" name="firstSide" value="0">

<td>

</tr>

<tr>

<td>

Input second side:

</td>

<td>

<input type="textbox" name="secondSide" value="0">

<td>

</tr>

<tr>

<td>

Input third side:

</td>

<td>

<input type="textbox" name="thirdSide" value="0">

<td>

</tr>

<tr><td colspan="2"><input type="button" name="submitButton" value="Triangle calculation" onclick="calculate()"></td></tr>

</table>

<p id='possible'></p>

<p id='rightTriangle'></p>

</form>

</body>

</html>

## Comments

## Leave a comment