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) {.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
total += numFill in the Blanks
Spot the Error
Student with a class code? Join your class →