JavaScript Simplified / Chapter 28 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

A variable that starts out with an initial value, and then is switched to a different value under certain conditions is a _____. flag flag
The values true and false are _________ values. Boolean [Bb]oolean
Set the initial value of a flag, isNegativeNumber, to the positive Boolean. let isNegativeNumber = true; letisNegativeNumber=true;
Reverse the value of isNegativeNumber, whose initial value was true. isNegativeNumber = false; isNegativeNumber=false;
Set the initial value of a flag that hasn't been previously declared. Make up the flag name and its Boolean value. let checkedOut = true; or let checkedOut = false; let[a-z_$][a-zA-Z0-9_$]*=(true|false);
When the following code runs, how many times will the "Sorry" alert display before the "Congratulations" alert displays? Answer with a number, like 1. let popDogs = ["Poodle", "Sheltie", "Lab", "Husky", "Pug"];
let breedToCheck = "Husky";
for (let i = 0; i < 5; i++) {
  if (breedToCheck === popDogs[i]) {
    alert("Congratulations!");
    break;
  } else {
    alert("Sorry");
  }
}
3 3
Code an alert that executes after the loop is finished. The alert says "Sorry" if no match has been found. let matchFound = false;
for (let i = 0; i < 5; i++) {
  if (breedToCheck === popDogs[i]) {
    alert("Congratulations!");
    matchFound = true;
    break;
  }
}
if (matchFound === false) {
  alert("Sorry");
}
if\(matchFound===false\){[\r\n]alert\(•Sorry•\);[\r\n]}
In the following loop, code the missing statement. let matchFound = false;
for (let i = 0; i < 5; i++) {
  if (breedToCheck === popDogs[i]) {
    alert("Congratulations!");
    ___________________
    break;
}
matchFound = true; matchFound=true;
  1. Assign a Boolean to a variable.
  2. Code an if statement: If the variable's value is either true or false (it will be, of course), display 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, the alert will display.
  6. Dismiss the alert by clicking OK.
  7. For help with this code, see Chapter 28 in the book.
  1. Leave my code intact.
  2. Complete the loop.
  3. If a match is found...
  4. ...the flag is reversed and...
  5. ...an alert displays the new value of the flag.
  6. Click the Result button (or, after revising, don't click, just wait).
  7. Wait a moment.
  8. If you've coded correctly, an alert will display "true".
  9. Dismiss the alert by clicking OK.
  10. For help with this code, see Chapter 28 in the book.