# 2.7 Game 2302-0370 motion -- uniform and relative velocity  (Page 6/11)

 Page 6 / 11

Screen output

The text shown in Figure 5 should appear in your browser window when you open the html file in your browser.

Figure 5 . Screen output for Listing #3.
```Start Script Velocity magnitude = 3.00 miles/hourVelocity angle = 0.00 degrees End Script```

Analysis of the code

As before, the script begins by defining the getAngle function, which doesn't require further explanation.

The vectorSum function

As you can see from Listing 3 , this script contains a new function named vectorSum . The purpose of this function is to add two vectors, given the magnitude and angle (in degrees) for each vector asincoming parameters. (The function assumes that both incoming magnitude parameters are expressed in the same units.)

The need to add two vectors occurs so often in physics that I decided to encapsulate the capability into a function that we can copy into future scripts.That will relieve us of the need to rewrite the code each time we need that functionality.

Return an array object

The magnitude and the angle (in degrees) of the resultant vector is returned in a two-element array object, with the magnitude in the element at index 0 and the angle in the element at index 1.I will have a more to say about arrays shortly.

Note that this function calls the getAngle function, which must also be provided in the script.

Subtraction of vectors

The function can also be used to subtract one vector from another. To subtract one vector from another, simply add 180 degrees to the angle for the subtrahend vector before passing the angle to the function.

Although the function can only be used to add two vectors, it can be called repeatedly to add any number of vectors two at a time.

To use this function to add more than two vectors, add the first two vectors as normal. Then add the vector output from the function to the third vector, etc., until all of the vectors have been included in the sum.

The code in the vectorSum function

Most of the code in the function is the same as code that I have explained in earlier modules. However, there are a couple of lines of code that aredifferent.

The function begins with the following statement:

var vecResult = new Array(2);

The purpose of this statement is to create a two-element array object named vecResult .

What is an array object?

You can think of an array object as a software object containing pigeon holes into which you can store data and from which you can later retrieve that data. This particular arrayobject has two pigeon holes, and they are numbered 0 and 1.

This array object will be used as a container to return the magnitude and angle values for the resultant vector.

Can only return one item

A JavaScript function can only return one item. Up to this point in these modules, that item has consisted of a single numeric value, such as the anglevalue that is returned from the getAngle function.

However, the vectorSum function needs to return two different values. One way to do that is to put those two values in the pigeon holes of an array object andreturn that array object as the one item that can be returned.

how do I set up the problem?
what is a solution set?
Harshika
find the subring of gaussian integers?
Rofiqul
hello, I am happy to help!
Abdullahi
find the value of 2x=32
divide by 2 on each side of the equal sign to solve for x
corri
X=16
Michael
Want to review on complex number 1.What are complex number 2.How to solve complex number problems.
Beyan
use the y -intercept and slope to sketch the graph of the equation y=6x
how do we prove the quadratic formular
hello, if you have a question about Algebra 2. I may be able to help. I am an Algebra 2 Teacher
thank you help me with how to prove the quadratic equation
Seidu
may God blessed u for that. Please I want u to help me in sets.
Opoku
what is math number
4
Trista
x-2y+3z=-3 2x-y+z=7 -x+3y-z=6
Need help solving this problem (2/7)^-2
x+2y-z=7
Sidiki
what is the coefficient of -4×
-1
Shedrak
the operation * is x * y =x + y/ 1+(x × y) show if the operation is commutative if x × y is not equal to -1
An investment account was opened with an initial deposit of \$9,600 and earns 7.4% interest, compounded continuously. How much will the account be worth after 15 years?
lim x to infinity e^1-e^-1/log(1+x)
given eccentricity and a point find the equiation
A soccer field is a rectangle 130 meters wide and 110 meters long. The coach asks players to run from one corner to the other corner diagonally across. What is that distance, to the nearest tenths place.
Jeannette has \$5 and \$10 bills in her wallet. The number of fives is three more than six times the number of tens. Let t represent the number of tens. Write an expression for the number of fives.
What is the expressiin for seven less than four times the number of nickels
How do i figure this problem out.
how do you translate this in Algebraic Expressions
why surface tension is zero at critical temperature
Shanjida
I think if critical temperature denote high temperature then a liquid stats boils that time the water stats to evaporate so some moles of h2o to up and due to high temp the bonding break they have low density so it can be a reason
s.
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Got questions? Join the online conversation and get instant answers! By Dakota Bocan By Joanna Smithback By Rhodes By Michael Nelson By OpenStax By Anonymous User By Jams Kalo By Richley Crapo By Hope Percle By OpenStax