Arrays – reduce()

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

Arrays – reduce()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does reduce() combine all elements into a single value?
A Yes
B No
2. Does reduce() return a new array like map() does?
A No
B Yes
3. Is a return statement needed inside reduce()'s function?
A No
B Yes
4. Can reduce() take a starting value as a second argument?
A No
B Yes
5. Is 'Reduce' with a capital R valid JavaScript syntax?
A No
B Yes

Match the Pairs

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

Left columnRight column
1nums.reduce(function(total, n) { return total + n; }, 0) AMultiplies every number in nums into a single product
2nums.reduce(function(total, n) { return total * n; }, 1) BAdds up every number in nums into a single total
3Combines every element into a single result value CWrong - 'reduce' must start with a lowercase r
4Sets the accumulator's value before the loop begins DWrong - missing the 'return' keyword
5nums.Reduce(function(t, n) { return t+n; }, 0) E0
6nums.reduce(function(t, n) { t + n; }, 0) Farray.reduce(function(total, item) { ... })

Definitions

reduce()

Fill in the Blanks

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

1.
let sum = nums.______(function(total, n) {
2.
let sum = nums.reduce(________(total, n) {
3.
______ total + n;
4.
let product = nums.______ (function(total, n) {
5.
let product = nums.reduce(function(total, n) { ______ total * n; }, 1);

Spot the Error

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

Question 1
A let a = [1, 2, 3];
B let b = a.Reduce(function(t, n) {
C return t + n;
Question 2
A let c = [1, 2, 3];
B let d = c.reduce(function(t, n) {
C t + n;
Question 3
A let e = [1, 2, 3];
B let f = e.map(function(t, n) {
C return t + n;
Question 4
A let g = [1, 2, 3];
B let h = g.reduce(function(t, n) {
C return t + n
D }, 0);
Question 5
A let i = [1, 2, 3];
B let j = i.reduce(function(t, n) {
C return t + n;
D ], 0);

Answer Key

Arrays – reduce()
Section A: Quick Questions
Q1
A — Yes
Q2
A — No
Q3
B — Yes
Q4
B — Yes
Q5
A — No
Section B: Match the Pairs
Q1
nums.reduce(function(total, n) { return total + n; }, 0) → Adds up every number in nums into a single total
Q2
nums.reduce(function(total, n) { return total * n; }, 1) → Multiplies every number in nums into a single product
Q3
Combines every element into a single result value → array.reduce(function(total, item) { ... })
Q4
Sets the accumulator's value before the loop begins → 0
Q5
nums.Reduce(function(t, n) { return t+n; }, 0) → Wrong - 'reduce' must start with a lowercase r
Q6
nums.reduce(function(t, n) { t + n; }, 0) → Wrong - missing the 'return' keyword
Section C: Definitions
Q1
reduce() — Combines every element of an array into a single value using an accumulator function.
Section D: Fill in the Blanks
Q1
let sum = nums.reduce(function(total, n) {
Q2
let sum = nums.reduce(function(total, n) {
Q3
return total + n;
Q4
let product = nums.reduce (function(total, n) {
Q5
let product = nums.reduce(function(total, n) { return total * n; }, 1);
Section E: Spot the Error
Q1
B — 'reduce' must start lowercase - a capital R causes an error.
Q2
C — Missing the 'return' keyword - without it, nothing is carried forward.
Q3
B — map() doesn't combine values into one - use reduce() instead.
Q4
C — This line is missing a semicolon, but that's not required in JS.
Q5
D — The block must close with }, not ].