Common bugs in functions and arrays

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

This Common bugs in functions and arrays worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (JavaScript) journey. Print it as a booklet?

Common bugs in functions and arrays

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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++)
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; }
3. Where should the total be set to 0 when adding up an array?
A Before the loop starts
B Inside the loop body
4. You want to run the function greet. Which line actually calls it?
A greet;
B greet();
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

Match the Pairs

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

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

Definitions

bug

Fill in the Blanks

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

1.
for (let i = 0; i _ scores.length; i++) { console.log(scores[i]); }
2.
function double(n) { ______ n * 2; }
3.
let total = _; for (const n of nums) { total += n; } console.log(total);
4.
function greet() { return "Hi"; } console.log(greet_));
5.
for (let i = 0; i __ nums.length - 1; i++) { console.log(nums[i]); }

Spot the Error

Shade in the letter of the line that contains an 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 }
Question 2
A function cube(n) {
B const result = n * n * n;
C console.log("Cubing...");
D result;
E }
F console.log(cube(3));
Question 3
A function sumAll() {
B let total = 0;
C for (const n of nums) {
D total += n;
E }
F return total;
Question 4
A const bonus = () => 10;
B const total = 50 + bonus;
C console.log(total);
Question 5
A function isAdult(age) {
B const ok = age >= 18;
C console.log(ok);
D }
E console.log(isAdult(20));

Answer Key

Common bugs in functions and arrays
Section A: Quick Questions
Q1
A — for (let i = 0; i < scores.length; i++)
Q2
A — function add(a, b) { return a + b; }
Q3
A — Before the loop starts
Q4
B — greet();
Q5
B — undefined, because the last index is 4
Section B: Match the Pairs
Q1
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.
Q2
function add(a, b) { a + b; } → Missing return: the sum is worked out but never sent back, so add(2, 3) gives undefined.
Q3
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; }
Q4
To run a function and get its result you must call it with brackets, even if it has no parameters. → const score = getScore();
Q5
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.
Q6
console.log(getScore); → The function is not called because the () are missing, so it prints the function itself, not its result.
Section C: Definitions
Q1
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 ().
Section D: Fill in the Blanks
Q1
for (let i = 0; i < scores.length; i++) { console.log(scores[i]); }
Q2
function double(n) { return n * 2; }
Q3
let total = 0; for (const n of nums) { total += n; } console.log(total);
Q4
function greet() { return "Hi"; } console.log(greet());
Q5
for (let i = 0; i <= nums.length - 1; i++) { console.log(nums[i]); }
Section E: Spot the Error
Q1
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.
Q2
D — The function works out the answer but never returns it, so cube(3) gives undefined. The line should be return result;
Q3
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) {
Q4
B — Without () the function is not called. JavaScript adds the function itself to 50 instead of its result. It should be 50 + bonus();
Q5
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;