Arrays – forEach()

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

Arrays – forEach()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does forEach() call a function once for each array element?
A No
B Yes
2. Is 'forEach' spelled with a capital E in the middle?
A No
B Yes
3. Does forEach() need a function passed to it?
A Yes
B No
4. Is 'ForEach' with a capital F valid JavaScript syntax?
A No
B Yes
5. Does forEach() return a new array?
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
1fruits.forEach(function(fruit) { ... }) APrints every number in nums, one at a time
2nums.forEach(function(n) { console.log(n); }) BCalls the function once for each element in fruits
3Runs a function once for every element in an array CWrong - missing the 'function' keyword before the parameter
4Holds the current element's value during each call Darray.forEach(function(item) { ... })
5fruits.ForEach(function(fruit) { ... }) EWrong - 'forEach' must start with a lowercase f
6fruits.forEach(fruit) { ... } Ffruit

Definitions

forEach()

Fill in the Blanks

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

1.
fruits._______(function(fruit) {
2.
fruits.forEach(________(fruit) {
3.
colors._______(function(color) {
4.
colors.forEach(________(color) {
5.
nums._______(function(n) {

Spot the Error

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

Question 1
A a.ForEach(function(x) {
B let a = [1, 2];
C console.log(x);
Question 2
A let b = [1, 2];
B b.foreach(function(x) {
C console.log(x);
Question 3
A let c = [1, 2];
B c.forEach(function(x) {
C console.log(x)
D });
Question 4
A let d = [1, 2, 3];
B d.forEach(function(x) {
C console.log(x);
D ]
Question 5
A let e = [1, 2];
B console.log(e);
C let f2 = 3;
D e.forEach((x) {
E console.log(x);

Answer Key

Arrays – forEach()
Section A: Quick Questions
Q1
B — Yes
Q2
B — Yes
Q3
A — Yes
Q4
A — No
Q5
A — No
Section B: Match the Pairs
Q1
fruits.forEach(function(fruit) { ... }) → Calls the function once for each element in fruits
Q2
nums.forEach(function(n) { console.log(n); }) → Prints every number in nums, one at a time
Q3
Runs a function once for every element in an array → array.forEach(function(item) { ... })
Q4
Holds the current element's value during each call → fruit
Q5
fruits.ForEach(function(fruit) { ... }) → Wrong - 'forEach' must start with a lowercase f
Q6
fruits.forEach(fruit) { ... } → Wrong - missing the 'function' keyword before the parameter
Section C: Definitions
Q1
forEach() — Calls a given function once for every element in an array.
Section D: Fill in the Blanks
Q1
fruits.forEach(function(fruit) {
Q2
fruits.forEach(function(fruit) {
Q3
colors.forEach(function(color) {
Q4
colors.forEach(function(color) {
Q5
nums.forEach(function(n) {
Section E: Spot the Error
Q1
A — 'forEach' must start lowercase - capital F causes an error.
Q2
B — 'forEach' needs a capital E - 'foreach' is wrong casing.
Q3
C — This line is missing a semicolon, but that's not required in JS.
Q4
D — The block must close with }, not ].
Q5
D — Missing the 'function' keyword before the parameter.