Topics for the quiz

The fourth quiz will cover if-else statements in JavaScript

I will ask two types of questions. In the first type of question I will show you some JavaScript code that works with one or more variables and ask you predict the contents of one or more variables when the code is finished executing. In the second type of question I will ask you to write out some JavaScript code.

Sample questions

Here are some examples of the kinds of problems I may pose.

1. Here is some JavaScript code.

var x, y, z;

x = 25;
y = 13;
z = 10;

if(x < y) {
  if(x < z) {
    z = y - x;
  } else {
    x = y - z;
  }
} else {
  if(y < z) {
    z = x - y;
  } else {
    y = z - x;
  }
}

What are the final values of the variables x, y, and z after this code executes?

Answer:

x = 25
y = -15
z = 10

2. A movie theater charges different admission based on customer's ages. Customers 18 or under pay $6.50, customers 65 and over pay $5.50, and everyone else pays $7.50. In a JavaScript program we have stored a customer's age in a variable age. Write an if-else statement that computes the amount that customer will pay for admission. Store the admission charge value in a variable named admission.

Answer:

if(age <= 18)
  admission = 6.50;
else if(age >= 65)
  admission = 5.50;
else
  admission = 7.50;

3.Rectangle one has dimensions a by b inches. Rectangle two has dimensions c by d inches. Write a JavaScript if-else statement that can determine whether or not rectangle two will fit inside rectangle one. If it can, set the value of the variable named fits to 'True', if it can not fit, set the value of the variable named fits to 'False'. Note that it may be possible to fit rectangle two into rectangle one by rotating rectangle two by ninety degreees.

Answer:

fits = 'False';
if(c < a && d < b)
  fits = 'True';
else if(c < b && d < a)
  fits = 'True';