Passing an array to a function

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

Passing an array to a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which call gives the function total ONE array of scores?
A total([4, 7, 9])
B total(4, 7, 9)
2. You have let myScores = [4, 7, 9]; Which call passes this array to total?
A total(myScores[])
B total(myScores)
3. In function total(scores) { ... }, what is scores?
A A number that stores how many items are in the array
B A parameter that holds the array passed in when the function is called
4. Which loop header reads every item of the array scores inside the function?
A for (let i = 0; i < scores.length; i++)
B for (let i = 0; i < scores; i++)
5. What does this print? function first(list) { return list[0]; } console.log(first([5, 6, 7]));
A 5
B [5, 6, 7]

Match the Pairs

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

Left columnRight column
1function total(scores) { let sum = 0; for (const s of scores) sum += s; return sum; } AEmpty square brackets after the parameter name are invalid syntax. The array should be used by name or an index.
2function addBonus(scores) { scores.push(10); } BThe function receives an array in its parameter and loops over it to add up every item.
3Call a function and give it an array as the argument CThe function changes the original array that was passed in by adding an item to it.
4A function returns a new array and leaves the original array unchanged Dtotal([4, 7, 9]);
5function total(scores) { let sum = 0; for (const s of scores) sum += s; } EThere is no return statement, so the function gives back undefined instead of the total.
6function largest(scores) { return Math.max(scores[]); } Ffunction doubleAll(nums) { return nums.map(n => n * 2); }

Definitions

function total(scores)

Fill in the Blanks

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

1.
function count(list) { return list.______; }
2.
function double(nums) { return nums.___(n => n * 2); }
3.
function addItem(list, item) { list.____(item); }
4.
const scores = [4, 7, 9]; addItem(______, 5); console.log(scores);
5.
function show(names) { for (const n __ names) console.log(n); }

Spot the Error

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

Question 1
A function total(nums) {
B let sum = 0;
C nums.forEach(n => sum += n);
D return sums;
E }
Question 2
A function average(nums {
B let sum = 0;
C nums.forEach(n => sum += n);
D return sum / nums.length;
E }
Question 3
A function doubleAll(nums) {
B nums.map(n => n * 2);
C }
D const a = [1, 2, 3];
E console.log(doubleAll(a));
Question 4
A function bonusAll(marks) {
B let n = marks.length;
C for (let i = 0; i < n; i++) {
D let x = marks[i] + 5;
E }
F }
Question 5
A const marks = [70, 85, 60];
B function largest(nums) {
C return Math.max(nums);
D }
E console.log(largest(marks));

Answer Key

Passing an array to a function
Section A: Quick Questions
Q1
A — total([4, 7, 9])
Q2
B — total(myScores)
Q3
B — A parameter that holds the array passed in when the function is called
Q4
A — for (let i = 0; i < scores.length; i++)
Q5
A — 5
Section B: Match the Pairs
Q1
function total(scores) { let sum = 0; for (const s of scores) sum += s; return sum; } → The function receives an array in its parameter and loops over it to add up every item.
Q2
function addBonus(scores) { scores.push(10); } → The function changes the original array that was passed in by adding an item to it.
Q3
Call a function and give it an array as the argument → total([4, 7, 9]);
Q4
A function returns a new array and leaves the original array unchanged → function doubleAll(nums) { return nums.map(n => n * 2); }
Q5
function total(scores) { let sum = 0; for (const s of scores) sum += s; } → There is no return statement, so the function gives back undefined instead of the total.
Q6
function largest(scores) { return Math.max(scores[]); } → Empty square brackets after the parameter name are invalid syntax. The array should be used by name or an index.
Section C: Definitions
Q1
function total(scores) — A function with a parameter that receives an array when called, e.g. total([4, 7, 9]). The function can loop over it, return a new array, or change the original array.
Section D: Fill in the Blanks
Q1
function count(list) { return list.length; }
Q2
function double(nums) { return nums.map(n => n * 2); }
Q3
function addItem(list, item) { list.push(item); }
Q4
const scores = [4, 7, 9]; addItem(scores, 5); console.log(scores);
Q5
function show(names) { for (const n of names) console.log(n); }
Section E: Spot the Error
Q1
D — The variable is called sum, not sums, so this causes a ReferenceError. The function should return sum, the total built up while looping over the array.
Q2
A — The closing bracket after the parameter name is missing. It should be function average(nums) {.
Q3
B — map() builds a new array, but without return the function throws it away and gives back undefined. It should be return nums.map(n => n * 2);.
Q4
D — This stores the new mark in a local variable, so the original array is never changed. To change the array passed in, it should be marks[i] += 5;.
Q5
C — Math.max needs separate numbers, so passing the whole array gives NaN. The array must be spread with ...nums: Math.max(...nums).