← ACARA Digital Technologies Years 9–10

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;.

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 loop correctly visits every item in the array scores?
for (let i = 0; i < scores.length; i++)for (let i = 0; i <= scores.length; i++)
Matches
for (const n of nums) { let total = 0; total += n; }
↓
The total is set to 0 inside the loop, so it resets every pass and ends up holding only the last number.
Applying
Meanings
Term
bug
Meaning
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 ().
Practice
function total(nums) {
Testing
Gaps
for (let i = 0; i scores.length; i++) {
Check Answer
Spots
2const n = nums.length;
3for (let i = 0; i <= n; i++) {

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

The 'Spider' badge — Web Weaver, 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 loop correctly visits every item in the array scores?
A. for (let i = 0; i < scores.length; i++) ✓
B. for (let i = 0; i <= scores.length; i++)
Indexes run from 0 to length - 1. Using <= makes the loop go one step too far and read scores[scores.length], which is undefined.
2. A function should give back the total of two numbers. Which version works?
A. function add(a, b) { return a + b; } ✓
B. function add(a, b) { a + b; }
Without return, the function gives back undefined. The sum is worked out and then thrown away.
3. Where should the total be set to 0 when adding up an array?
A. Before the loop starts ✓
B. Inside the loop body
If let total = 0 is inside the loop, the total resets on every pass. It ends up holding only the last item instead of the sum.
4. You want to run the function greet. Which line actually calls it?
A. greet;
B. greet(); ✓
Without (), you only refer to the function itself. The code inside it never runs.
5. An array has 5 items. What does arr[5] give?
A. The fifth item in the array
B. undefined, because the last index is 4 ✓
Indexes start at 0, so a 5-item array uses 0 to 4. Reading arr[5] goes past the end and gives undefined.

Match the Pairs

for (let i = 0; i <= arr.length; i++) → Loop limit bug: <= runs one step too far, so the last pass reads arr[arr.length], which is undefined.
function add(a, b) { a + b; } → Missing return: the sum is worked out but never sent back, so add(2, 3) gives undefined.
A total must be set to 0 once, before the loop, so it keeps adding up across every pass. → let total = 0; for (const n of nums) { total += n; }
To run a function and get its result you must call it with brackets, even if it has no parameters. → const score = getScore();
for (const n of nums) { let total = 0; total += n; } → The total is set to 0 inside the loop, so it resets every pass and ends up holding only the last number.
console.log(getScore); → The function is not called because the () are missing, so it prints the function itself, not its result.

Key Term

bug
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 ().

Fill in the Blanks

1. for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Hint: Type the comparison operator that stops the loop before it reads past the last item.
2. function double(n) { return n * 2; }
Hint: Type the keyword that sends a value back to the code that called the function.
3. let total = 0; for (const n of nums) { total += n; } console.log(total);
Hint: Type the starting value for the total, which is set once before the loop, not inside it.
4. function greet() { return "Hi"; } console.log(greet());
Hint: Type the opening bracket that calls the function so it runs, instead of printing the function itself.
5. for (let i = 0; i <= nums.length - 1; i++) { console.log(nums[i]); }
Hint: Type the operator that includes the last valid index when the limit is length minus one.

Spot the Error

Question 1
A const nums = [4, 7, 9];
B const n = nums.length;
C for (let i = 0; i <= n; i++) {
D console.log(nums[i]);
E }
Line C — The last valid index is n - 1, so i <= n runs one time too many and prints undefined. The condition should be i < n.
Question 2
A function cube(n) {
B const result = n * n * n;
C console.log("Cubing...");
D result;
E }
F console.log(cube(3));
Line D — The function works out the answer but never returns it, so cube(3) gives undefined. The line should be return result;
Question 3
A function sumAll() {
B let total = 0;
C for (const n of nums) {
D total += n;
E }
F return total;
Line A — The parameter nums is missing, so the name nums does not exist inside the function and the loop causes a ReferenceError. It should be function sumAll(nums) {
Question 4
A const bonus = () => 10;
B const total = 50 + bonus;
C console.log(total);
Line B — Without () the function is not called. JavaScript adds the function itself to 50 instead of its result. It should be 50 + bonus();
Question 5
A function isAdult(age) {
B const ok = age >= 18;
C console.log(ok);
D }
E console.log(isAdult(20));
Line C — console.log only shows the value. It does not send it back to the caller, so isAdult(20) is undefined. It should be return ok;

Student with a class code? Join your class →