1
2
3
4
5
6
7
8
9
10
To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to CodePen.
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]} | |
|
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]} | |
|
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]} |
|
|||
|