<< Chapter < Page Chapter >> Page >

Declare and initialize variables

The code in Listing 1 begins by declaring and initializing variables named velocity and distance . Note that the units are shown in the comments. I recommend that you always dothat in order to keep the units straight.

Perform the arithmetic

Then Listing 1 declares a variable named time and sets its value equal to distance divided by velocity . Once again, note the treatment of the units in the comments, showing that the units for the result of the division are hours.

Display the results

Finally, Listing 1 calls the document.write method several times in succession to display the results shown in Figure 1 .

An exercise on average velocity

A hockey puck is struck by a player causing it to travel northeast at a uniform velocity of 50 feet per second for 15 feet, at which point it is struck by another player.

When struck by the second player, the direction of motion is changed to northwest witha uniform velocity of 60 feet per second.

It is stopped by a third player after traveling 20 feet in the northwest direction.

  • What is the time required for the puck to complete each leg of its journey?
  • What is the average velocity of the puck from the start to the end of its journey?

Vector diagram for the hockey puck

Figure 2 shows a vector diagram for the hockey puck.

Figure 2 - Vector diagram for the hockey puck.

Missing image.

Let's write a script to solve the problem

Please copy the code from Listing 2 into an html file and open it in your browser.

Listing 2 . An exercise on average velocity.

<!---------------- File JavaScript02.html ---------------------><html><body><script language="JavaScript1.3">document.write("Start Script<br/>"); //The purpose of this function is to receive the adjacent// and opposite side values for a right triangle and to // return the angle in degrees in the correct quadrant.function getAngle(x,y){ if((x == 0)&&(y == 0)){ //Angle is indeterminate. Just return zero.return 0; }else if((x == 0)&&(y>0)){ //Avoid divide by zero denominator.return 90; }else if((x == 0)&&(y<0)){ //Avoid divide by zero denominator.return -90; }else if((x<0)&&(y>= 0)){ //Correct to second quadrantreturn Math.atan(y/x)*180/Math.PI + 180; }else if((x<0)&&(y<= 0)){ //Correct to third quadrantreturn Math.atan(y/x)*180/Math.PI + 180; }else{//First and fourth quadrants. No correction required. return Math.atan(y/x)*180/Math.PI;}//end else }//end function getAngle//Compute the time required to traverse each leg. Identify // the two legs as A and B.var vecAmag = 15;//displacement in feet var vecAang = 45;//displacement angle in degreesvar vecAvel = 50;//velocity magnitude in feet per second var timeA = vecAmag/vecAvel;//feet/(feet/second) = secondsvar vecBmag = 20;//displacement in feet var vecBang = 135;//displacement angle in degreesvar vecBvel = 60;//velocity magnitude in feet per second var timeB = vecBmag/vecBvel;//feet/(feet/second) = seconds//Compute the overall displacement //Compute the horizontal and vertical components// of each vector. var vecAh = vecAmag*Math.cos(vecAang*Math.PI/180);var vecAv = vecAmag*Math.sin(vecAang*Math.PI/180); var vecBh = vecBmag*Math.cos(vecBang*Math.PI/180);var vecBv = vecBmag*Math.sin(vecBang*Math.PI/180); //Compute the sums of the horizontal and vertical// components from the two vectors to get the // horizontal and vertical component of the// resultant vector. var vecResultH = vecAh + vecBh;var vecResultV = vecAv + vecBv; //Use the Pythagorean theorem to compute the magnitude of the// resultant vector in feet. var vecResultMag = Math.sqrt(Math.pow(vecResultH,2) +Math.pow(vecResultV,2)); //Compute the angle of the resultant vector in degrees.vecResultAng = getAngle(vecResultH,vecResultV); var totalTime = timeA + timeB;//secondsvar vecVelMag = vecResultMag/totalTime;//feet/second var vecVelAng = vecResultAng;//degreesdocument.write("Time for leg A = " + timeA.toFixed(2) + " seconds<br/>"); document.write("Time for leg B = " + timeB.toFixed(2) +" seconds<br/>"); document.write("Displacement magnitude = " +vecResultMag.toFixed(2) + " feet<br/>"); document.write("Displacement angle = " +vecResultAng.toFixed(2) + " degrees<br/>"); document.write("Total time = " + totalTime.toFixed(2) +" seconds<br/>"); document.write("Average velocity magnitude = " +vecVelMag.toFixed(2) + " feet/second<br/>"); document.write("Average velocity angle = " +vecVelAng.toFixed(2) + " degrees<br/>"); document.write("End Script");</script></body></html>

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask