In this article, we will create the application that calculates the area of the triangle on the screen by using these values, which are requested and graded by the user with the base area and height.
To write our code, we create an empty text file and write the following 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="border1" placeholder="Enter Base Length"> <input type="text" id="border2" placeholder="Enter Height"> <input type="button" value="Calculate" id="calc"> <script> function calculate(){ //We read the values in the text box and pass them to number1, number2. var num1=document.getElementById("border1").value; var num2=document.getElementById("border2").value; //The values entered in the text box are textual. We're converting to numbers to make calculations. num1=Number(num1); num2=Number(num2); var area=num1*num2/2; alert(" Area of Triangle:"+area); } //we select the calculator button to perform the calculation. var calcBtn=document.getElementById("calc"); //append the function to the event. calcBtn.onclick=calculate; </script> |
Then we change the txt extension of our text file to html and run it.