for loops nested Flashcards
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.
15
The outer loop and inner loop can’t share the same ______. Answer with one word.
counter
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; } }
indent
for (var i = 0; i < 3; i++) { for (var j = 0; j < 10; j++) { if (j >= 1) { break; } } }
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
The correct code is: for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { alert(i + j); } }
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.
Sorry, that's incorrect. An example: for (var j = 0; j < 10; j++) { }
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
Sorry, that’s incorrect. An example:
for (var j = 0; j < 10; j++) {
alert(“Around and around we go.”);
}
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.
Sorry, that's incorrect. An example: for (var j = 0; j < someArray.length; j++) { cost = 99; }
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.
Sorry, that's incorrect. An example: for (var i = 0; i < 5; i++) { for (var j = 0; j < 5; j++) { } }
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.
for (var i = 0; i < 5; i++) { for (var j = 0; j < 5; j++) { total = i + j; } }
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.
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++;
}
}
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.
for (var j = 0; j < 10; j++) { }