Chapter 1 Exercise Set 1

Modulus practice

Evaluate the following modulus expressions in your head, then use a JavaScript console to check their values:

  1. 5 % 2
  2. 9 % 5
  3. 6 % 6
  4. 15 % 12
  5. 12 % 15
  6. 0 % 7
  7. 7 % 0

Boolean values

Determine whether the following expressions are true or false:

  1. !true || !false
    
  2. (true && !false) && (false || !true)
    
  3. (4 > 5) || true
    
  4. ("this" != "that") && ("zebra" > "aardvark")
    
  5. ((4 >= 6) || ("grass" != "green")) && !(((12 * 2) == 144) && true)
    

while loop 1

Use a while loop to write a program that calculates and shows the value of 210 (2 to the 10th power).

for loop 1

Rewrite the solution of the previous exercise using a for loop instead of a while loop.

Math.floor(n.d)

Run the following and record the result:

alert(Math.floor(3.25));

Invesigate the function Math.floor(...) by conducting a series of “experiments”, calling it with different arguments until you can describe what it does.

Math.random()

Run the following several times, recording the result after each “experiment”:

alert(Math.random());

Compare your results with those of your classmates. Are your results the same or different? Keep running new experiments until you think you understand what Math.random() does. Write a sentence that describes what this function does.

Random between low and high

Combine what you learned from the previous two exercise to create an expression using both Math.random and Math.floor to generate a random integer between two variables, high and low, where high is greater than low.

What’s 2 + 2?

Use the expression you created in the previous exercise to generate two random integers, opr1 and opr2 (short for “operand”). Then use these two new variables to modify the program in the break statement section to use your new random integers instead of 2 + 2.