Using the loop counter inside a for loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Using the loop counter inside a for loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does this code print? for (let i = 1; i <= 3; i++) { console.log(i); }
A 1, 2, 3 (one number per line)
B 1, 1, 1 (the same number three times)
2. Which loop prints the 7 times table: 7, 14, 21?
A for (let i = 1; i <= 3; i++) { console.log(7); }
B for (let i = 1; i <= 3; i++) { console.log(i * 7); }
3. What is the first number printed by this loop? for (let i = 0; i < 3; i++) { console.log(i); }
A 1
B 0
4. Which statement about the counter i is true?
A i holds a different value on each repeat of the loop
B i keeps the same value for the whole loop
5. Where can you use the variable i from this loop? for (let i = 1; i <= 3; i++) { console.log(i); }
A Only inside the loop's curly braces
B Anywhere in the program, including after the loop

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1for (let i = 1; i <= 3; i++) { console.log(i); } Afor (let i = 1; i <= 3; i++) { } console.log(i);
2for (let i = 1; i <= 5; i++) { console.log(i * 7); } Bi is in quotes, so the text i is printed five times instead of the counter values 1 to 5. Remove the quotes: console.log(i).
3Starting the counter at 0 means the first value used in the loop body is 0 CPrints the 7 times table: 7, 14, 21, 28, 35
4The counter i is local to the loop, so using it after the loop causes a ReferenceError DThe loop body never uses i, so it prints 7 every time. It should be console.log(i * 7) to get a different answer each repeat.
5for (let i = 1; i <= 5; i++) { console.log("i"); } EPrints 1, then 2, then 3, because the counter i changes on every repeat
6for (let i = 1; i <= 5; i++) { console.log(7); } Ffor (let i = 0; i < 3; i++) { console.log(i); }

Definitions

console.log(i)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for (let i = 1; i <= 5; i++) { console.log(_); }
2.
for (let i = 1; i <= 12; i++) { console.log(i _ 7); }
3.
for (let i = _; i < 3; i++) { console.log(i); }
4.
for (___ i = 1; i <= 3; i++) { console.log(i); }
5.
for (let i = 1; i <= 12; i++) { console.log(`${i} x 7 = ${_ * 7}`); }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A for (let i = 1; i <= 5; i++) {
B let doubled = i * 2;
C console.log(double);
D }
Question 2
A for (let i = 1; i <= 12; i++) {
B let answer = i * 7;
C console.log(i + ' x 7 = ' + 7);
D }
Question 3
A for (let i = 1; i <= 5; i--) {
B console.log(i);
C }
Question 4
A for (let i = 1; i <= 3; i++) {
B console.log(i);
C }
D console.log(i);
Question 5
A let sum = 0;
B for (let i = 1; i <= 5; i++) {
C sum += i;
D console.log(i + ': ' sum);
E }

Answer Key

Using the loop counter inside a for loop
Section A: Quick Questions
Q1
A — 1, 2, 3 (one number per line)
Q2
B — for (let i = 1; i <= 3; i++) { console.log(i * 7); }
Q3
B — 0
Q4
A — i holds a different value on each repeat of the loop
Q5
A — Only inside the loop's curly braces
Section B: Match the Pairs
Q1
for (let i = 1; i <= 3; i++) { console.log(i); } → Prints 1, then 2, then 3, because the counter i changes on every repeat
Q2
for (let i = 1; i <= 5; i++) { console.log(i * 7); } → Prints the 7 times table: 7, 14, 21, 28, 35
Q3
Starting the counter at 0 means the first value used in the loop body is 0 → for (let i = 0; i < 3; i++) { console.log(i); }
Q4
The counter i is local to the loop, so using it after the loop causes a ReferenceError → for (let i = 1; i <= 3; i++) { } console.log(i);
Q5
for (let i = 1; i <= 5; i++) { console.log("i"); } → i is in quotes, so the text i is printed five times instead of the counter values 1 to 5. Remove the quotes: console.log(i).
Q6
for (let i = 1; i <= 5; i++) { console.log(7); } → The loop body never uses i, so it prints 7 every time. It should be console.log(i * 7) to get a different answer each repeat.
Section C: Definitions
Q1
console.log(i) — Inside a for loop, the counter i changes on every repeat, so using it in the loop body gives a different value each time, like 1, 2, 3. It only exists inside the loop.
Section D: Fill in the Blanks
Q1
for (let i = 1; i <= 5; i++) { console.log(i); }
Q2
for (let i = 1; i <= 12; i++) { console.log(i * 7); }
Q3
for (let i = 0; i < 3; i++) { console.log(i); }
Q4
for (let i = 1; i <= 3; i++) { console.log(i); }
Q5
for (let i = 1; i <= 12; i++) { console.log(`${i} x 7 = ${i * 7}`); }
Section E: Spot the Error
Q1
C — The variable is called doubled, not double. Using a name that was never created causes a ReferenceError. It should be console.log(doubled);
Q2
C — This prints 7 on every repeat because it uses the number 7 instead of the answer variable, which changes with i. It should end with + answer);
Q3
A — i-- makes i go down (1, 0, -1...) so it never gets past 5 and the loop never ends. To count up it should be i++.
Q4
D — i is local to the loop and only exists inside the curly braces. Using it after the loop causes a ReferenceError. This line should be something that does not use i, like console.log('Done');
Q5
D — There is a + missing between the text ': ' and sum, which is a syntax error. It should be console.log(i + ': ' + sum);