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

Code the first line of a switch statement. Make up the variable name. switch (num) { switch\([a-z_$][a-zA-Z0-9_$]*\){
Code the second line of a switch statement that's testing for a string. Make up the string. Don't bother to indent. case "abc": case•.*•:
Assuming there are no more statements to execute, fill in the blank. switch (petSnakeGender) {
  case "female":
    name = "Lydia";
    __________
break; break;
Type the statement that's missing from this code. switch (num) {
  case 12:
    alert("It's a dozen!");
  default:
    alert("It's not a dozen.");
break; break;
Type the line that says what happens when no tests have been successful. Don't bother to indent. default: default:
Code the first 4 lines of a switch statement that tests an integer variable. If the first condition is true, display an alert. Make up everything. switch (num) {
  case 12:
    alert("It's a dozen!");
    break;
switch\([a-z_$][a-zA-Z0-9_$]*\){[\r\n]case(0|-?[1-9][\d]*):[\r\n]alert\(•.*•\);[\r\n]break;
Code a switch statement that duplicates the functionality of this code. if (num === 0) {
  alert("none");
} else {
  alert("some");
}
switch (num) {
  case 0:
    alert("none");
    break;
  default:
    alert("some");
}
switch\(num\){[\r\n]case0:[\r\n]alert\(•none•\);[\r\n]break;[\r\n]default:[\r\n]alert\(•some•\);[\r\n]}
Code a switch statement that tests for a string. It contains one case and a default. If the case tests true, display an alert. If not, display another alert. Make up everything. switch (title) {
  case "president":
    alert("Very impressive!");
    break;
  default:
    alert("Maybe in your future");
}
switch\([a-z_$][a-zA-Z0-9_$]*\){[\r\n]case•.*•:[\r\n]alert\(•.*•\);[\r\n]break;[\r\n]default:[\r\n]alert\(•.*•\);[\r\n]}
  1. Assign a value to a variable.
  2. Code a switch statement with one case and a default.
  3. The case tests the variable for a value and displays an alert if it tests true.
  4. The default displays a different alert.
  5. In the first statement, play around with the value of the variable and see what the switch statement does with it.
  6. Click the Result button (or, after revising, don't click, just wait).
  7. Wait a moment after each change.
  8. If you've coded correctly, appropriate alerts will display.
  9. Dismiss the alerts by clicking OK.
  10. For help with this code, see Chapter 62 in the book.
  1. Create an array.
  2. Code a switch statement with 2 cases and a default.
  3. You're testing the value of one of the array's elements. If it's one value, display an alert. If it's another value, display a different alert. If it's neither, display a third alert.
  4. Play around with the values in the array and see how the switch statement responds.
  5. Click the Result button (or, after revising, don't click, just wait).
  6. Wait a moment after each change.
  7. If you've coded correctly, appropriate alerts will display.
  8. Dismiss the alerts by clicking OK.
  9. For help with this code, see Chapter 62 in the book.