Common bugs in functions and arrays
A mistake in code that gives wrong results. Common ones: a missing return, <= instead of < in an index loop, a total set inside the loop, or calling a function without ().
Understanding Common bugs in functions and arrays in ACARA Digital Technologies Years 9–10
What it does: Putting let total = 0 before the loop means it is not reset on each pass, so the total keeps growing correctly. getScore() runs the function and gives its return value. Writing getScore alone just refers to the function itself. Indexes run from 0 to length - 1. Using <= makes the loop go one step too far and read scores[scores.length], which is undefined. Indexes start at 0, so a 5-item array uses 0 to 4. Reading arr[5] goes past the end and gives undefined. Adding undefined to a number gives NaN. An off-by-one loop limit is a common cause. Camel casing is only a naming style and does not cause bugs.
Common mistakes: The last valid index is n - 1, so i <= n runs one time too many and prints undefined. The condition should be i < n. The function works out the answer but never returns it, so cube(3) gives undefined. The line should be return result;.
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
bugFill in the Blanks
Spot the Error
Student with a class code? Join your class →