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

If an outer loop runs 3 times and an inner loop runs 5 times, how many times will the inner loop iterate? Answer with a numeral, like 1. 15 15
In the following nonsensical code, how many times will the outer loop iterate? Answer with a numeral, like 1. (Even though the inner loop stops prematurely each time, there's nothing to stop the outer loop from iterating to its limit.) for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 10; j++) {
    if (j > 0) {
      break;
    }
  }
}
3 3
Code nested loops. Use i and j as counters. The outer loop runs 3 times. The inner loop runs 3 times each time the outer loop iterates. Use < to define the loop limits. With each iteration of the inner loop, an alert displays showing the sum of the two counters. for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    alert(i + j);
  }
}
for\(leti=0;i<3;i\+\+\){[\r\n]for\(letj=0;j<3;j\+\+\){[\r\n]alert\(i\+j\);[\r\n]}[\r\n]}
Code an inner loop, including indentation, that iterates without doing anything. Make up a counter other than i. Use the usual starting value. Run it 10 times for each outer loop iteration. Use < to define the loop limit. Increment by 1.   for (let j = 0; j < 10; j++) {
  }
for\(let([a-z_$][a-zA-Z0-9_$]*)=0;\1<10;\1\+\+\){[\r\n]}
Code an inner loop, including indentation, that displays a text message in an alert. Make up a counter other than i. Make up the message. The counter has the usual starting value. Run it 10 times for each outer loop iteration. Use < to define the loop limit. Increment by 1.   for (let j = 0; j < 10; j++) {
    alert("hi");
  }
for\(let([a-z_$][a-zA-Z0-9_$]*)=0;\1<10;\1\+\+\){[\r\n]alert\(•.*•\);[\r\n]}
Code nested loops that do nothing. Use i and j as counters. Start counters at zero. The outer loop runs 5 times. The inner loop runs 5 times each time the outer loop iterates. Use < to define the loop limits. Increment by 1. for (let i = 0; i < 5; i++) {
  for (let j = 0; j < 5; j++) {
  }
}
for\(leti=0;i<5;i\+\+\){[\r\n]for\(letj=0;j<5;j\+\+\){[\r\n]}[\r\n]}
  1. Code nested loops. Use i and j as counters.
  2. The inner loop adds the two counters and assigns the sum to total, a variable that has been declared beforehand.
  3. Start counters at zero.
  4. The outer loop runs 5 times. The inner loop runs 5 times each time the outer loop iterates.
  5. Use < to define the loop limit.
  6. Increment both counters by 1.
for (let i = 0; i < 5; i++) {
  for (let j = 0; j < 5; j++) {
    total = i + j;
  }
}
for\(leti=0;i<5;i\+\+\){[\r\n]for\(letj=0;j<5;j\+\+\){[\r\n]total=i\+j;[\r\n]}[\r\n]}
  1. Below are 3 lines of code. Complete the code to create nested loops.
  2. Use i and j as counters.
  3. Use < and the length keyword to define the loop limits.
  4. In a single statement, the inner loop concatenates each of the elements of animals with each of the elements of products, adding the combinations to foodItems using push.
let animals = ["goat ", "cat ", "crow "];
let products = ["milk", "cheese", "burger"];
let foodItems = [];
for (let i = 0; i < animals.length; i++) {
  for (let j = 0; j < products.length; j++) {
    foodItems.push(animals[i] + products[j]);
  }
}
for\(leti=0;i<(animals\.length);i\+\+\){[\r\n]for\(letj=0;j<(products\.length);j\+\+\){[\r\n]foodItems\.push\(animals\[i\]\+products\[j\]\);[\r\n]}[\r\n]}
  1. Code nested loops.
  2. The outer loop runs twice. The inner loop runs twice for each outer loop iteration.
  3. On each iteration of the inner loop, display the sum of the two counters in an alert.
  4. Click the Result button (or, after revising, don't click, just wait).
  5. Wait a moment.
  6. If you've coded correctly, the alerts will display.
  7. Dismiss the alert by clicking OK.
  8. For help with this code, see Chapter 30 in the book.
  1. Create two 2-element string arrays.
  2. Create an empty third array.
  3. Code nested loops that concatenate all the combinations of the array elements.
  4. After the loops have run, code an alert that displays the first element of the third array.
  5. Click the Result button (or, after revising, don't click, just wait).
  6. Wait a moment.
  7. If you've coded correctly, an alert will display the first element of the third array.
  8. Dismiss the alert by clicking OK.
  9. For help with this code, see Chapter 30 in the book.