In this article, we will create an application that will calculate and display the area of the triangle on the screen using JavaScript.
The program takes two numbers as the base and height values from the user. Then it calculate the area of triangle with these numbers and display the calculation result.
Output:
JavaScript Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <input type="text" id="b1" placeholder="Enter base number"> <input type="text" id="b2" placeholder="Enter height number"> <input type="button" value="Calculate" id="btnCalc"> <div id="result" style="width:150px; border: 1px solid #ddd;position: absolute;top:50px;left:50;padding:20px"> </div> <script> function area(){ var number1=document.getElementById("b1").value; var number2=document.getElementById("b2").value; var result=document.getElementById("result"); number1=Number(number1); number2=Number(number2); let area=number1*number2/2; result.innerHTML="Area of triangle:"+area } var btnCalc=document.getElementById("btnCalc"); btnCalc.onclick=area; </script> |