Page 386 - Web_Application_v2.0_C12_Fb
P. 386
<INPUT TYPE="number" ID="y1" PLACEHOLDER="y1">
<P>Point B:</P>
<INPUT TYPE="number" ID="x2" PLACEHOLDER="x2">
<INPUT TYPE="number" ID="y2" PLACEHOLDER="y2">
<BR><BR>
<BUTTON ONCLICK="calculateDistance()">Calculate Distance</BUTTON>
<P ID="result"></P>
<SCRIPT>
function calculateDistance() {
var x1 = parseFloat(document.getElementById("x1").value);
var y1 = parseFloat(document.getElementById("y1").value);
var x2 = parseFloat(document.getElementById("x2").value);
var y2 = parseFloat(document.getElementById("y2").value);
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
document.getElementById("result").innerHTML = "The distance between the two
points is: " + distance.toFixed(2);
}
</SCRIPT>
</BODY>
</HTML>
Output of the preceding code is as follows:
18. Write a JavaScript code to input the initial velocity, acceleration and time travelled. Calculate the distance
travelled by the body by using the formula:
Distance = initial Velocity × Time + (1/2) × Acceleration × Time
2
384 Touchpad Web Applications (Ver. 2.0)-XII

