Counting down with a for loop

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

Counting down with a for loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which for loop counts down from 10 to 1?
A for (let i = 10; i >= 1; i++)
B for (let i = 10; i >= 1; i--)
2. A countdown starts at 10 and should stop after printing 1. Which condition is correct?
A i >= 1
B i <= 1
3. What does this loop print? for (let i = 3; i >= 1; i--) { console.log(i); }
A 3, 2, 1
B 1, 2, 3
4. To count down, the starting value of i should be...
A Low, such as let i = 1
B High, such as let i = 10
5. Which update makes a for loop count down in steps of 2?
A i -= 2
B i += 2

Match the Pairs

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

Left columnRight column
1for (let i = 10; i >= 1; i--) Afor (let i = 10; i >= 1; i--)
2for (let i = 10; i >= 0; i -= 2) Bfor (let i = 10; i >= 1; i--) { console.log(i); }
3To count down, the loop condition must use >= so the loop keeps running while the counter is still at or above the last number CCounts down from 10 to 1, taking away 1 each time the loop repeats
4A countdown loop updates the counter by subtracting, so the counter gets smaller every time DThe condition i <= 1 is false straight away because 10 is not less than or equal to 1, so the loop never runs. It should be i >= 1.
5for (let i = 10; i >= 1; i++) EThe update i++ adds 1, so the counter goes up away from 1 and the loop never ends. It should be i--.
6for (let i = 10; i <= 1; i--) FCounts down from 10 to 0 in steps of 2: 10, 8, 6, 4, 2, 0

Definitions

for (let i = 10; i >= 1; i--)

Fill in the Blanks

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

1.
for (let i = __; i >= 1; i--) { console.log(i); }
2.
for (let i = 10; i __ 1; i--) { console.log(i); }
3.
for (let i = 10; i >= 1; ___) { console.log(i); }
4.
for (let i = 10; i >= 1; i __ 2) { console.log(i); }
5.
for (let i = 10; i >= 1; i--) { console.log(_); }

Spot the Error

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

Question 1
A console.log("Countdown");
B for (let i=10; i>=1; i++) {
C console.log(i);
D }
Question 2
A for (let i=1; i>=10; i--) {
B console.log(i);
C }
Question 3
A let count = 0;
B for (let i=10; i>=1; i--) {
C count--;
D }
E console.log(count);
Question 4
A let total = 0;
B for (let i=10; i>=1; i--) {
C total += i;
D console.log(totl);
E }
Question 5
A console.log("Evens down");
B for (let i=10; i>=2; i-=2) {
C console.log("i");
D }

Answer Key

Counting down with a for loop
Section A: Quick Questions
Q1
B — for (let i = 10; i >= 1; i--)
Q2
A — i >= 1
Q3
A — 3, 2, 1
Q4
B — High, such as let i = 10
Q5
A — i -= 2
Section B: Match the Pairs
Q1
for (let i = 10; i >= 1; i--) → Counts down from 10 to 1, taking away 1 each time the loop repeats
Q2
for (let i = 10; i >= 0; i -= 2) → Counts down from 10 to 0 in steps of 2: 10, 8, 6, 4, 2, 0
Q3
To count down, the loop condition must use >= so the loop keeps running while the counter is still at or above the last number → for (let i = 10; i >= 1; i--) { console.log(i); }
Q4
A countdown loop updates the counter by subtracting, so the counter gets smaller every time → for (let i = 10; i >= 1; i--)
Q5
for (let i = 10; i >= 1; i++) → The update i++ adds 1, so the counter goes up away from 1 and the loop never ends. It should be i--.
Q6
for (let i = 10; i <= 1; i--) → The condition i <= 1 is false straight away because 10 is not less than or equal to 1, so the loop never runs. It should be i >= 1.
Section C: Definitions
Q1
for (let i = 10; i >= 1; i--) — A for loop that counts down: start i high, keep going while i >= 1, and subtract 1 each time with i--. Use i -= 2 to count down in steps of 2.
Section D: Fill in the Blanks
Q1
for (let i = 10; i >= 1; i--) { console.log(i); }
Q2
for (let i = 10; i >= 1; i--) { console.log(i); }
Q3
for (let i = 10; i >= 1; i--) { console.log(i); }
Q4
for (let i = 10; i >= 1; i -= 2) { console.log(i); }
Q5
for (let i = 10; i >= 1; i--) { console.log(i); }
Section E: Spot the Error
Q1
B — i++ adds 1 each time, so i goes up (11, 12, ...) and i >= 1 stays true forever. A countdown must use i-- to subtract 1 each time.
Q2
A — A countdown must start high. Starting at 1 makes i >= 10 false straight away, so the loop never runs. It should be let i=10 and i>=1.
Q3
C — The loop runs 10 times, and count should go up by 1 each time with count++. Using count-- would give -10 instead of 10.
Q4
D — The variable is spelled total. Writing totl causes a ReferenceError because that name was never created.
Q5
C — Quotes make "i" a piece of text, so the letter i prints every time. Remove the quotes to print the counter value: console.log(i);