A function that totals and averages an array

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

A function that totals and averages an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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;
2. Which line adds each item to the total inside a for...of loop?
A total += num;
B total = num;
3. Where should 'return total;' go in a totalling function?
A After the loop has finished
B Inside the loop, after total += num
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
5. Which expression gives the average of the array nums once total is calculated?
A total / nums
B total / nums.length

Match the Pairs

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

Left columnRight column
1let total = 0; Alet total = 0 is inside the loop, so the total resets to 0 on every pass. It must be declared once before the loop.
2return total / nums.length; BSets the running total to 0 before the loop starts, so each array item can be added to it.
3A loop adds every item in the array to the running total Ctotal + num works out a value but never stores it. It should be total += num so the total is updated.
4Check for an empty array first so you never divide by zero Dfor (const num of nums) { total += num; }
5for (const num of nums) { let total = 0; total += num; } EDivides the total by the number of items in the array to give the average.
6for (const num of nums) { total + num; } Fif (nums.length === 0) return 0;

Definitions

total += num

Fill in the Blanks

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

1.
function sumArray(nums) { let total = _; for (const num of nums) total += num;
2.
for (const num of nums) { total __ num; }
3.
let total = 0; for (const n of nums) total += n; return total _ nums.length;
4.
if (nums.length ___ 0) { return 0; }
5.
const sum = sumArray(nums); return sum / nums.______;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A function sumAll(nums) {
B let total = 1;
C for (const n of nums)
D total += n;
E return total;
F }
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 }
Question 3
A function addUp(nums) {
B let total = 0;
C for (const n of nums)
D total = n;
E return total;
F }
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 }
Question 5
A function sumScores(scores) {
B const total = 0;
C for (const s of scores)
D total += s;
E return total;
F }

Answer Key

A function that totals and averages an array
Section A: Quick Questions
Q1
A — let total = 0;
Q2
A — total += num;
Q3
A — After the loop has finished
Q4
B — total resets to 0 on every pass, so the result is wrong
Q5
B — total / nums.length
Section B: Match the Pairs
Q1
let total = 0; → Sets the running total to 0 before the loop starts, so each array item can be added to it.
Q2
return total / nums.length; → Divides the total by the number of items in the array to give the average.
Q3
A loop adds every item in the array to the running total → for (const num of nums) { total += num; }
Q4
Check for an empty array first so you never divide by zero → if (nums.length === 0) return 0;
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
function sumArray(nums) { let total = 0; for (const num of nums) total += num;
Q2
for (const num of nums) { total += num; }
Q3
let total = 0; for (const n of nums) total += n; return total / nums.length;
Q4
if (nums.length === 0) { return 0; }
Q5
const sum = sumArray(nums); return sum / nums.length;
Section E: Spot the Error
Q1
B — The running total must start at 0. Starting at 1 makes every answer one too big. It should be let total = 0;
Q2
A — The parameter list is missing its closing bracket. It should be function average(nums) {
Q3
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.
Q4
A — JavaScript arrow functions use => not ->. It should be const average = (nums) => {
Q5
B — total changes inside the loop, so it cannot be declared with const. It should be let total = 0; so it can be updated.