JavaScript Simplified / Chapter 31 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 for…of loop is a good way to loop over an array, string, or other _______. iterable iterable
The first keyword inside the parentheses of a for…of loop can be let or ____. const const
The name of the array is arr. Fill in the blank. for (const elem __________) { of arr ofarr
The name of the string is str. The variable is char. Code the first line of the for…of loop using const. for (const char of str) { for\(constcharofstr\){
Below is an array. Code the first line of a for…of loop that iterates through the array. Make up the name of the variable. Use const. let arr = [12, 24, 36]; for (const elem of arr) { for\(const[a-z_$][a-zA-Z0-9_$]*ofarr\){
Code a complete for…of loop that displays, in turn, each element of an array in the console. Make up the names of the array and the variable. Use const. for (const elem of arr) {
  console.log(elem);
}
for\(const([a-z_$][a-zA-Z0-9_$]*)of[a-z_$][a-zA-Z0-9_$]*\){[\r\n]console\.log\(\1\);[\r\n]}
Code a complete for…of loop that displays, in turn, each element of an integer array in the console. If a particular integer turns up, skip to the next element. Make up everything. Use const. for (const elem of arr) {
  if (elem === 0) {
    continue;
  }
  console.log(elem);
}
for\(const([a-z_$][a-zA-Z0-9_$]*)of[a-z_$][a-zA-Z0-9_$]*\){[\r\n]if\(\1===(0|-?[1-9][\d]*)\){[\r\n]continue;[\r\n]}[\r\n]console\.log\(\1\);[\r\n]}
Code a complete for…of loop that displays, in turn, each character of a string. If a particular character turns up, end the loop. Make up everything. Use const. for (const elem of arr) {
  if (elem === "z") {
    break;
  }
  console.log(elem);
}
for\(const([a-z_$][a-zA-Z0-9_$]*)of[a-z_$][a-zA-Z0-9_$]*\){[\r\n]if\(\1===•.*•\){[\r\n]break;[\r\n]}[\r\n]console\.log\(\1\);[\r\n]}
  1. I've coded an array. Leave my code intact.
  2. Code a for…of loop that displays, in turn, each element of the array.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, alerts will display each of the strings in turn.
  6. Dismiss each alert by clicking OK.
  7. For help with this code, see Chapter 31 in the book.
  1. I've coded a string. Leave my code intact.
  2. Code a for…of loop that displays, in turn, each character except for the incorrect character. Use the continue keyword.
  3. Click the Result button (or, after revising, don't click, just wait).
  4. Wait a moment.
  5. If you've coded correctly, alerts will display each of the correct characters in turn.
  6. Dismiss each alert by clicking OK.
  7. For help with this code, see Chapter 31 in the book.