JavaScript Simplified / Chapter 39 Exercises

  • Index of exercises
  • Email me

1

2

3

4

5

6

7

8

9

10

Congratulations. You've aced all the exercises for this chapter.


To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to CodePen.


Are you ready to rate JavaScript Simplified? on Amazon?


Rate it on Amazon.

0,1,2,3,4,5,6,7,8,9

0

0

0

0

Fill in the blank to generate a pseudo-random number. let randomNum = ___________(); Math.random Math\.random
The Math.random() function has generated a pseudo-random number, assigned to the variable bigDecimal. Fill in the blank to produce a number from 1.0000000000000000 through 50.9999999999999999 let improvedNum = bigDecimal * __ + 1; 50 50
Generate a pseudo-pseudo-random number and assign it to the variable randNum that hasn't been declared yet. let randNum = Math.random(); letrandNum=Math\.random\(\);
You want a pseudo-random number that ranges from 1 to 100. You've multiplied by 100 and added 1. The number has been assigned to y. Code the last step, assigning the result to x, which hasn't been declared beforehand. let x = Math.floor(y); letx=Math\.floor\(y\);
Generate a pseudo-random number and assign it to a variable that hasn't been declared beforehand. Then code the first line of an if statement that checks whether the number is greater than 5. Make up the name of the variable. let x = Math.random();
if (x > 5 ) {
let([a-z_$][a-zA-Z0-9_$]*)=Math\.random\(\);[\r\n]if\(\1>5\){
You want a number that ranges from 1 to 10. Code the first step in converting a big decimal that has been assigned to a variable. Assign the result to a second variable that hasn't been declared beforehand. Make up the variable names. let convertedNum = num * 10 + 1; let[a-z_$][a-zA-Z0-9_$]*=[a-z_$][a-zA-Z0-9_$]*\*10\+1;
Code the last step in converting a pseudo-random number to an integer. Assign the result to the same variable that represents the number you're converting. Make up the variable name. num = Math.floor(num); ([a-z_$][a-zA-Z0-9_$]*)=Math\.floor\(\1\);
Code all three statements that generate a pseudo-random integer ranging from 1 to 6. Make up three variable names. let num1 = Math.random();
let num2 = num1 * 6 + 1;
let num3 = Math.floor(num2);
let([a-z_$][a-zA-Z0-9_$]*)=Math\.random\(\);[\r\n]let([a-z_$][a-zA-Z0-9_$]*)=\1\*6\+1;[\r\n]let[a-z_$][a-zA-Z0-9_$]*=Math\.floor\(\2\);
  1. Code a statement that generates a pseudo-random decimal—the first step in creating a pseudo-random integer.
  2. Display the number in an alert.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, an alert will display the decimal.
  6. Dismiss the alert by clicking OK.
  7. For help with this code, see Chapter 39 in the book.
  1. Generate a pseudo-random number, an integer, from 1 to 3.
  2. Display the number in an alert.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, an alert will display a number from 1 to 3.
  6. Dismiss the alert by clicking OK.
  7. For help with this code, see Chapter 39 in the book.