Computation With Variables

advertisement
Computation with Variables
Corresponding Material
Basic JavaScript, Lesson 4
Exercises: T-Shirt Shop, Running Speed
Discussion
Variables are not only useful because they store
values. We can use variables to perform operations
like addition, subtraction, and multiplication. In this
handout, we’ll create a few variables and use them to
perform some basic computations.
Exercise
Let’s create a few variables to work with. What’s your
favorite number?
var favoriteNumber = _____ ;
On what day of the month were you born?
var dayOfBirth = _____ ;
How many books have you read this year?
var numBooksRead = _____ ;
Lastly, we’ll create an answer variable to store the
results of our computations.
var answer = 0;
Now, fill in the blanks in the table below!
Step
Pseudocode
Answer
Add day of birth and books
read, assign the result to
the answer variable
Subtract the favorite
number from the value of
answer, store the new
result in the answer again
Multiply answer by the day
of birth, store the value in
the answer variable
Subtract the number of
books read from the
answer and assign this
value to the answer
variable
What’s your total answer? _____________
Download