for loops nested Flashcards

1
Q

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.

A

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The outer loop and inner loop can’t share the same ______. Answer with one word.

A

counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What's missing from this code? Answer with a 6-letter word that starts with "i".
for (var i = 0; i <= array1Length; i++) {
for (var j = 0; j < array2Length; j++) {
  sumOfCounters = i + j;
}
}
A

indent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
for (var i = 0; i < 3; i++) {
  for (var j = 0; j < 10; j++) {
    if (j >= 1) {
      break;
    } 
  }
}
A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
The correct code is:
for (var i = 0; i < 3; i++) {
  for (var j = 0; j < 3; j++) {
    alert(i + j);
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code an inner loop, including indentation, that doesn’t do anything. Use any counter other than i. Use the usual starting value. Run it 10 times for each outer loop iteration. Increment by 1.

A
Sorry, that's incorrect. An example:
  for (var j = 0; j < 10; j++) {
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code an inner loop, including indentation, that displays a text message in an alert. Use any counter other than i. Declare it with the usual starting value. Run it 10 times for each outer loop iteration. Use

A

Sorry, that’s incorrect. An example:
for (var j = 0; j < 10; j++) {
alert(“Around and around we go.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Code an inner loop, including indentation, that assigns a number to a variable that has been declared beforehand. Use any counter other than i. Declare it with the usual starting value. Limit iterations by the number of elements in an array. Increment by 1.

A
Sorry, that's incorrect. An example:
  for (var j = 0; j < someArray.length; j++) {
    cost = 99;
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Code nested loops that do nothing. Start counters at zero. The outer loop runs 5 times. The inner loop runs 5 times each time the outer loop iterates. Increment by 1.

A
Sorry, that's incorrect. An example:
for (var i = 0; i < 5; i++) {  
  for (var j = 0; j < 5; j++) {
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Code nested loops. The inner loop adds the two counters and assigns the sum to a variable that has been declared beforehand. Start counters at zero. The outer loop runs 5 times. The inner loop runs 5 times each time the outer loop iterates. Increment by 1.

A
for (var i = 0; i < 5; i++) {  
  for (var j = 0; j < 5; j++) {
    total = i + j; 
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
var animals = ["goat", "cat", "crow"];
var products = ["milk", "cheese", "burger"];
var foodItems = [];
var k = 0;
Continue the code to create nested loops. The inner loop concatenates each of the elements of animals with each of the elements of products, with no space separating them. Accumulate the combinations in foodItems. The counter for foodItems is k. Limit the number of loops by the length of each array.
A

Sorry, that’s incorrect. An example:
for (var i = 0; i < animals.length; i++) {
for (var j = 0; j < products.length; j++) {
foodItems[k] = animals[i] + products[j];
k++;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code an inner loop, including indentation, that doesn’t do anything. Use any counter other than i. Use the usual starting value. Run it 10 times for each iteration of the outer loop. Increment by 1.

A
for (var j = 0; j < 10; j++) {
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly