Now we're going to write JavaScript code to do basic computations. REMEMBER: Do not just
go through the steps--make sure you really understand what is going on!
1. Instead of creating a new JavaScript file each time we try something new, copy the alert from the
example on the previous page into another blank text file. This text file will not be a
JavaScript file, but will contain all the code you wrote in this lab. Annotate the file with
notes for yourself. Something like:
Lab #6:
First JavaScript program:
alert("Hello, world!");
2. Save the file as lab6.txt. You will NOT be submitting this
file. This is for your own records.
3. Delete the contents of your JavaScript file. Your lab6.js should
be empty again.
4. Store your age in a variable (with a meaningful variable name). Display your age in a popup
box. For example, if you are 19, have the popup box say: I am 19 years old.
You will need to understand string concatenation. Refer to the slides if you are unsure about
how to do this step.
After you are done writing the code, refresh (Ctrl+R) your HTML page in the browser. You should see
a popup box with the correct message.
5. Let's compute how old you will be when you graduate. Add another variable that stores how
many years you have left until you graduate. Then have a popup box appear that says how
old you will be when you graduate, something like: I will be 22 when I
graduate. Do NOT add a third variable when computing your graduating age--have JavaScript
do the math for you. Hint: You will need parentheses, so that the math portion does not mix with
the string concatenation.
REMEMBER: As you try different things out, make sure you understand WHY each wrong way is wrong
and WHY each right way is right.
6. Consider copying what you have now into your lab6.txt text file.
7. Suppose you want to get a Master's degree. That means 2 more years of schooling. Change
the value of a single VARIABLE, so that if you refresh the HTML page, the message
I will be X when I graduate will now have an age that is 2 years
more than before.
8. Copy what you have now into your lab6.txt text file. Erase
everything in the JavaScript file.
9. Create two variables with the year you entered the UW and the second variable has the
year you expect to graduate from the UW. The UW wants everyone to graduate in 4 years to keep
the flow of students moving. Write an if
statement that checks if the second
variable's value is within 4 years of the first variable's value. If so, a popup box should
appear saying You are on track. Otherwise, the popup box
should say When are you going to leave?. Play around with the
values in the variables to see if your conditional is working.