Create a function to calculate and return the area of a triangle, given the leng
ID: 3675853 • Letter: C
Question
Create a function to calculate and return the area of a triangle, given the lengths of the sides. Put the function in a JavaScript file (Heron.js"). In an HTML file ("Exercise 7.html"), create a main script to test the function, as follows: Give a context message for the user (finding the area of a triangle, given the lengths of its sided). Get the lengths of the sides of the triangle from the user Call the function, passing it the three lengths and store the resulting area in a variable. If the area is Not a Number or the area is zero, output an error message (These three numbers cannot be the sides of a triangle.") Otherwise, output the area, as "The area of the triangle is 72.93485" The function (named triangleArea) should use Heron's formula to calculate the area: A- Vs(s - a) (s - b)(s - ) where a, b, and c are the lengths of the sides of the triangle, and s is the semiperimeter: s (a+ b+ c)/2 Use the Math.sqrt function to calculate the square root. Don't forget to return the area. (Since users of Heron's formula are familiar with the use of a, b, c and s to represent the sides and semiperimeter as above, you may use these four letters as variable names in the function.)Explanation / Answer
Exercise7.html
<html>
<head><h3>Find The Area of the triangle given the length of its sided.<h3><br><br>
<script src="Heron.js">
</script>
</head>
<body>
<input type="submit" value="Calculate Area">
</body>
</html>
----------------------------------------------------------------------------------------------
Heron.js
function calculate() {
var sides = prompt("Enter Triangle side lengths in cm (number,number,number)"),
nsides = sides.split(","),
a = parseFloat(nsides[0]),
b = parseFloat(nsides[1]),
c = parseFloat(nsides[2]),
s = (a + b + c) / 2,
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
if(area==0 || isNaN(area))
{
alert("These Three sides cannot be the sides of the traingle");
}
else
{
alert("The triangle's area is " + area + " square cm");
}
return area; // return the area
}
------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.