Arrays – filter()

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

Arrays – filter()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does filter() create a new array of only matching elements?
A Yes
B No
2. Does filter() change the original array?
A Yes
B No
3. Is a return statement needed inside filter()'s function?
A Yes
B No
4. Does map() filter out elements the way filter() does?
A Yes
B No
5. Is 'Filter' with a capital F 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.filter(function(n) { return n > 2; }) AWrong - missing the 'return' keyword
2people.filter(function(p) { return p.age >= 18; }) BCreates a new array of only the adult people
3Runs a function on every element to keep matches Creturn
4Decides whether an element passes the test DWrong - 'filter' must start with a lowercase f
5nums.Filter(function(n) { return n>2; }) ECreates a new array with only numbers over 2
6nums.filter(function(n) { n > 2; }) Farray.filter(function(item) { ... })

Definitions

filter()

Fill in the Blanks

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

1.
let big = nums.______(function(n) {
2.
let big = nums.filter(________(n) {
3.
______ n % 2 === 0;
4.
let adults = people.______(function(p) {
5.
let adults = people.filter(function(p) { ______ p.age >= 18; });

Spot the Error

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

Question 1
A a.Filter(function(n) {
B let a = [1, 2, 3];
C return n > 1;
Question 2
A n2filter(function(n) {
B let n2 = [1, 2];
C return n > 1;
Question 3
A let e = [1, 2, 3];
B let f = e.map(function(n) {
C return n > 1;
Question 4
A let o = [1, 2];
B let p = o.fitler(function(n) {
C return n > 1;
Question 5
A let c = [1, 2, 3];
B let d = c.filter(function(n) {
C n > 1;

Answer Key

Arrays – filter()
Section A: Quick Questions
Q1
A — Yes
Q2
B — No
Q3
A — Yes
Q4
B — No
Q5
A — No
Section B: Match the Pairs
Q1
nums.filter(function(n) { return n > 2; }) → Creates a new array with only numbers over 2
Q2
people.filter(function(p) { return p.age >= 18; }) → Creates a new array of only the adult people
Q3
Runs a function on every element to keep matches → array.filter(function(item) { ... })
Q4
Decides whether an element passes the test → return
Q5
nums.Filter(function(n) { return n>2; }) → Wrong - 'filter' must start with a lowercase f
Q6
nums.filter(function(n) { n > 2; }) → Wrong - missing the 'return' keyword
Section C: Definitions
Q1
filter() — Creates a new array containing only the elements that pass a test provided by a function.
Section D: Fill in the Blanks
Q1
let big = nums.filter(function(n) {
Q2
let big = nums.filter(function(n) {
Q3
return n % 2 === 0;
Q4
let adults = people.filter(function(p) {
Q5
let adults = people.filter(function(p) { return p.age >= 18; });
Section E: Spot the Error
Q1
A — 'filter' must start lowercase - a capital F causes an error.
Q2
A — Missing the dot before 'filter' - it should be n2.filter(...).
Q3
B — map() doesn't filter elements - use filter() instead.
Q4
B — 'filter' is misspelled as 'fitler'.
Q5
C — Missing the 'return' keyword - without it, nothing is tested.