Arrays – map()

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

Arrays – map()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does map() create a new array?
A No
B Yes
2. Does the original array change after calling map()?
A No
B Yes
3. Is a return statement needed inside map()'s function?
A Yes
B No
4. Does forEach() return a new array like map() does?
A No
B Yes
5. Is 'Map' with a capital M valid JavaScript syntax?
A Yes
B No

Match the Pairs

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

Left columnRight column
1nums.map(function(n) { return n * 2; }) AWrong - 'map' must start with a lowercase m
2users.map(function(u) { return u.name; }) BCreates a new array of just the users' names
3Runs a function on every element to build a new array CCreates a new array with each number doubled
4Sends the transformed value back into the new array Darray.map(function(item) { ... })
5nums.Map(function(n) { return n * 2; }) EWrong - missing the 'return' keyword
6nums.map(function(n) { n * 2; }) Freturn

Definitions

map()

Fill in the Blanks

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

1.
let doubled = nums.___(function(n) {
2.
let doubled = nums.map(________(n) {
3.
______ n * 2;
4.
let names = users.___(function(u) {
5.
let squares = nums.map(function(n) { ______ n * n; });

Spot the Error

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

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

Answer Key

Arrays – map()
Section A: Quick Questions
Q1
B — Yes
Q2
A — No
Q3
A — Yes
Q4
A — No
Q5
B — No
Section B: Match the Pairs
Q1
nums.map(function(n) { return n * 2; }) → Creates a new array with each number doubled
Q2
users.map(function(u) { return u.name; }) → Creates a new array of just the users' names
Q3
Runs a function on every element to build a new array → array.map(function(item) { ... })
Q4
Sends the transformed value back into the new array → return
Q5
nums.Map(function(n) { return n * 2; }) → Wrong - 'map' must start with a lowercase m
Q6
nums.map(function(n) { n * 2; }) → Wrong - missing the 'return' keyword
Section C: Definitions
Q1
map() — Creates a new array by running a given function on every element of the original array.
Section D: Fill in the Blanks
Q1
let doubled = nums.map(function(n) {
Q2
let doubled = nums.map(function(n) {
Q3
return n * 2;
Q4
let names = users.map(function(u) {
Q5
let squares = nums.map(function(n) { return n * n; });
Section E: Spot the Error
Q1
B — 'map' must start lowercase - a capital M causes an error.
Q2
C — Missing the 'return' keyword - without it, nothing is sent back.
Q3
B — forEach() doesn't return a new array - use map() instead.
Q4
C — This line is missing a semicolon, but that's not required in JS.
Q5
D — The block must close with }, not ].