Anda di halaman 1dari 2

CSC574: DYNAMIC WEB APPLICATION DEVELOPMENT

ASSIGNMENT 1: LAB 5 (Using Mathematical Constants and Methods)


Prepared by: Hafizahtul Naimah binti Ammran (2015125769)

Use the document.write commands to display the output for each of the following JavaScript
programs (Note: Format your output).

1. Write a program that computes the duration of a projectiles flight and its height above
ground when it reaches the target using the formula given.

<!-- Hafizahtul Naimah binti Ammran (2015125769)


ASSIGNMENT 1: Using Mathematical Constants and Methods in
Javascript
QUESTION 1 -->
<html>
<head>
<title>FLIGHT CALCULATION SYSTEM</title>
</head>

<body>
<script>
var g = 9.8
var e = 20
var v = 245
var d = 3.5
var t, h

d = d * 1000 //convert km to m

t = d / (v * Math.cos(e))
t = t.toFixed(2) //fixed into 2 decimal places

h = v * Math.sin(e) * t - ((g * Math.pow(t,2))/2)


h = h.toFixed(2) //fixed into 2 decimal places

var output = "The duration of the projectile's flight, T, is " +


t + " seconds."
var output2 = "The height above ground, H, is "+ h +" meters."

document.write(output + "<br>" + output2)


</script>
</body>
</html>
2. The Cartesians coordinates of two points are (3,7) and (8,12). Write a program to calculate
and display:
a. the slope of the line connecting the two points. Use the fact that the slope between
two points having coordinates (x1, y1) and (x2, y2) is
slope = (y2 y1) / (x2 x1)
b. the coordinates of the midpoint of the line connecting the two points. Use the fact
that the coordinates of the midpoint are
([(x1 + x2) / 2], [(y1 + y2) / 2])
c. (iii) the distance of the two points, computed using the following formula:
distance = (x1 - x2 )2 + (y1 - y2 )2

<!-- Hafizahtul Naimah binti Ammran (2015125769)


ASSIGNMENT 1: Using Mathematical Constants and Methods in
Javascript
QUESTION 2 -->
<html>
<head>
<title>CARTESIAN COORDINATE</title>
</head>

<body>
Point 1: (3,7)<br>
Point 2: (8,12)<br>
<br>
<script>
var x1 = 3, y1 = 7, x2 = 8, y2 = 12
var slope, mx, my, mid, distance

slope = (y2-y1)/(x2-x1)
mx = (x1+x2)/2
my = (y1+y2)/2
distance = Math.sqrt(Math.pow(x1-x2,2) + Math.pow(y1-y2,2))

document.write("Slope = " + slope + "<br>")


document.write("Midpoint = (" + mx +", " + my +")<br>")
document.write("Distance = " + distance.toFixed(2))
</script>
</body>
</html>

Anda mungkin juga menyukai