← ACARA Digital Technologies Years 9–10

A function that totals and averages an array

Adds each array item to a running total inside a loop. Set let total = 0 before the loop, then return total. Divide by nums.length for the average.

Understanding A function that totals and averages an array in ACARA Digital Technologies Years 9–10

What it does: The for...of loop visits each item once, and total += num adds that item to the running total. An empty array has length 0, so returning early stops the average from dividing by 0 and giving NaN. If return is inside the loop, the function stops after the first item. Put it after the loop so every item is added first. Use === to compare. A single = assigns a value instead of comparing, which is a bug. The total must start at 0 before the loop, otherwise there is nothing valid to add the items to. The average is the sum of all the items divided by how many items there are, which is nums.length.

Common mistakes: The running total must start at 0. Starting at 1 makes every answer one too big. It should be let total = 0;. The parameter list is missing its closing bracket. It should be function average(nums) {.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 710–727) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line should come BEFORE the loop in a function that totals an array?
let total = 0;let total = nums[0] + 1;
Matches
for (const num of nums) { let total = 0; total += num; }
↓
let total = 0 is inside the loop, so the total resets to 0 on every pass. It must be declared once before the loop.
Applying
Meanings
Term
total += num
Meaning
Adds each array item to a running total inside a loop. Set let total = 0 before the loop, then return total. Divide by nums.length for the average.
Practice
function average(nums) {
Testing
Gaps
let total = ;
Check Answer
Spots
1function sumAll(nums) {
2 let total = 1;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hyena' badge — Savannah Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line should come BEFORE the loop in a function that totals an array?
A. let total = 0; ✓
B. let total = nums[0] + 1;
The running total must start at 0 so the first item is added to nothing. Then each loop adds one item.
2. Which line adds each item to the total inside a for...of loop?
A. total += num; ✓
B. total = num;
+= adds num to what total already holds. Plain = would throw away the old total each time.
3. Where should 'return total;' go in a totalling function?
A. After the loop has finished ✓
B. Inside the loop, after total += num
If return is inside the loop, the function stops after the first item. Put it after the loop so every item is added first.
4. What happens if you write 'let total = 0;' INSIDE the loop?
A. The function works the same way
B. total resets to 0 on every pass, so the result is wrong ✓
The total must be set up once before the loop. Inside the loop it is wiped each time, so only the last item counts.
5. Which expression gives the average of the array nums once total is calculated?
A. total / nums
B. total / nums.length ✓
The average is the total divided by how many items there are. nums.length gives that count. Dividing by the array itself gives NaN.

Match the Pairs

let total = 0; → Sets the running total to 0 before the loop starts, so each array item can be added to it.
return total / nums.length; → Divides the total by the number of items in the array to give the average.
A loop adds every item in the array to the running total → for (const num of nums) { total += num; }
Check for an empty array first so you never divide by zero → if (nums.length === 0) return 0;
for (const num of nums) { let total = 0; total += num; } → let total = 0 is inside the loop, so the total resets to 0 on every pass. It must be declared once before the loop.
for (const num of nums) { total + num; } → total + num works out a value but never stores it. It should be total += num so the total is updated.

Key Term

total += num
Adds each array item to a running total inside a loop. Set let total = 0 before the loop, then return total. Divide by nums.length for the average.

Fill in the Blanks

1. function sumArray(nums) { let total = 0; for (const num of nums) total += num;
Hint: Type the starting value of the running total before the loop begins.
2. for (const num of nums) { total += num; }
Hint: Type the operator that adds each item onto the running total.
3. let total = 0; for (const n of nums) total += n; return total / nums.length;
Hint: Type the arithmetic operator that divides the total by the number of items to get the average.
4. if (nums.length === 0) { return 0; }
Hint: Type the comparison operator that checks whether the array's length is exactly zero.
5. const sum = sumArray(nums); return sum / nums.length;
Hint: Type the array property that gives how many items it holds.

Spot the Error

Question 1
A function sumAll(nums) {
B let total = 1;
C for (const n of nums)
D total += n;
E return total;
F }
Line B — The running total must start at 0. Starting at 1 makes every answer one too big. It should be let total = 0;
Question 2
A function average(nums {
B let total = 0;
C for (const n of nums)
D total += n;
E return total / nums.length;
F }
Line A — The parameter list is missing its closing bracket. It should be function average(nums) {
Question 3
A function addUp(nums) {
B let total = 0;
C for (const n of nums)
D total = n;
E return total;
F }
Line D — Using = replaces the total each time round the loop, so only the last item is kept. It should be total += n; to add each item to the total.
Question 4
A const average = (nums) -> {
B let total = 0;
C for (const n of nums)
D total += n;
E return total / nums.length;
F }
Line A — JavaScript arrow functions use => not ->. It should be const average = (nums) => {
Question 5
A function sumScores(scores) {
B const total = 0;
C for (const s of scores)
D total += s;
E return total;
F }
Line B — total changes inside the loop, so it cannot be declared with const. It should be let total = 0; so it can be updated.

Student with a class code? Join your class →