Homework #4


1. True or False: the following are valid JavaScript statements? (Answer each)
a. if (var == 4) { // Do something }
False, "var" is one of the reserved words you showed us in class, therefore the variable cannot be named "var".

b. var testVar = 10;
True.

c. if (a == b) { // Do something }
True.

d. testVar = 10;
True.

e. var case = “Yes”;
False, "case" is also one of the reserved words you showed us in class, therefore the variable cannot be names "case".

2. True or False: Semicolons are required to terminate every JavaScript statement.
False; if statements, loops, etc do not call for a semicolon.

3. Examine the following bit of JavaScript. What is the likely result? 1. var orderTotal = 0; 2. function collectOrder(numOrdered) { 3. if (numOrdered > 0) { 4. alert("You ordered " + orderTotal); 5. orderTotal = numOrdered * 5; 6. } 7. return orderTotal; 8. }
As long as the customer has ordered something there will be an alert pop up saying how many have been ordered. But line 4 and 5 are mixed up because the orderTotal is called in line 4 before it is updated in line 5.