Totals and counters in a loop

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

Totals and counters in a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should you set let total = 0 when adding up values in a loop?
A Before the loop starts
B Inside the loop body
2. Which line adds the value of i to a running total?
A total = i;
B total += i;
3. Which line counts one more time something happens?
A count = 1;
B count++;
4. A loop adds the numbers 1 to 4 to total, which starts at 0. What is total after the loop?
A 10
B 4
5. Where should console.log(total) go if you want to show the final total only once?
A After the closing brace of the loop
B Inside the loop, before the closing brace

Match the Pairs

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

Left columnRight column
1let total = 0; Alet total = 0 is inside the loop, so the total is reset to 0 on every pass and never builds up. It must be set before the loop.
2count++; Bcount = 1 sets the counter to 1 every time instead of adding 1, so it never goes above 1. It should be count++.
3Add each value in the loop to the running total Cconsole.log(total);
4Show the final total once, after the loop has finished DSets the running total to 0 before the loop starts, so every value can be added onto a clean start.
5for (let i = 1; i <= 5; i++) { let total = 0; total += i; } Etotal += i;
6let count = 0; for (let i = 1; i <= 5; i++) { count = 1; } FAdds 1 to the counter each time it runs, so it counts how many times something happened.

Definitions

total += value;

Fill in the Blanks

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

1.
let total = _; for (let i = 1; i <= 5; i++) total += i; console.log(total);
2.
let total = 0; for (let i = 1; i <= 5; i++) total __ i; console.log(total);
3.
let count = 0; for (let i = 0; i < 5; i++) _______; console.log(count);
4.
let total = 0; for (let i = 1; i <= 4; i++) total += i; ___________(total);
5.
let total = 0; for (let i = 1; i <= 3; i++) _____ += i * 2; console.log(total);

Spot the Error

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

Question 1
A let total = 0;
B for (let i = 1; i <= 5; i--) {
C total += i;
D }
E console.log(total);
Question 2
A let marks = [50, 80, 90];
B let total = 1;
C for (let m of marks) {
D total += m;
E }
F console.log(total);
Question 3
A const count = 0;
B let marks = [40, 75, 90, 55];
C for (let m of marks) {
D if (m >= 50) count++;
E }
F console.log(count);
Question 4
A let total = 0;
B let n = 1;
C while (n >= 5) {
D total += n;
E n++;
F }
Question 5
A let count = 0;
B for (let i = 0; i < 10; i++) {
C if (i > 6) count + 1;
D }
E console.log(count);

Answer Key

Totals and counters in a loop
Section A: Quick Questions
Q1
A — Before the loop starts
Q2
B — total += i;
Q3
B — count++;
Q4
A — 10
Q5
A — After the closing brace of the loop
Section B: Match the Pairs
Q1
let total = 0; → Sets the running total to 0 before the loop starts, so every value can be added onto a clean start.
Q2
count++; → Adds 1 to the counter each time it runs, so it counts how many times something happened.
Q3
Add each value in the loop to the running total → total += i;
Q4
Show the final total once, after the loop has finished → console.log(total);
Q5
for (let i = 1; i <= 5; i++) { let total = 0; total += i; } → let total = 0 is inside the loop, so the total is reset to 0 on every pass and never builds up. It must be set before the loop.
Q6
let count = 0; for (let i = 1; i <= 5; i++) { count = 1; } → count = 1 sets the counter to 1 every time instead of adding 1, so it never goes above 1. It should be count++.
Section C: Definitions
Q1
total += value; — Adds each value to a running total inside a loop. Start with let total = 0 before the loop, and use count++ to count how many times something happens. Print the results after the loop.
Section D: Fill in the Blanks
Q1
let total = 0; for (let i = 1; i <= 5; i++) total += i; console.log(total);
Q2
let total = 0; for (let i = 1; i <= 5; i++) total += i; console.log(total);
Q3
let count = 0; for (let i = 0; i < 5; i++) count++; console.log(count);
Q4
let total = 0; for (let i = 1; i <= 4; i++) total += i; console.log(total);
Q5
let total = 0; for (let i = 1; i <= 3; i++) total += i * 2; console.log(total);
Section E: Spot the Error
Q1
B — i-- makes the counter go down, so i <= 5 is always true and the loop never ends. It should be i++ so the loop counts up and finishes.
Q2
B — A total must start at 0 before the loop. Starting at 1 makes the final answer 1 too big. It should be let total = 0;
Q3
A — A counter changes every time count++ runs, so it can't be a const. It should be let count = 0; or the program crashes with a TypeError.
Q4
C — n starts at 1, so n >= 5 is false straight away and the loop body never runs. The total stays 0. It should be while (n <= 5) {
Q5
C — count + 1 works out a number but never stores it, so count stays 0. Use count++ (or count += 1) to update the counter.