Searching an array with includes and indexOf

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

This Searching an array with includes and indexOf worksheet is part of the ACARA Digital Technologies Years 9–10 — Modular and Object-Oriented Programming (JavaScript) journey. Print it as a booklet?

Searching an array with includes and indexOf

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line checks if "Ava" is in the array names and gives true or false?
A names.includes("Ava")
B names.indexOf("Ava")
2. let pets = ["cat", "dog"]; What does pets.includes("fish") give?
A -1
B false
3. let nums = [5, 8, 12]; What does nums.indexOf(12) give?
A 2
B true
4. let colours = ["red", "blue"]; What does colours.indexOf("green") give?
A -1
B 0
5. Which if statement correctly checks that "Leo" is NOT in the array team?
A if (team.indexOf("Leo") === 0)
B if (team.indexOf("Leo") === -1)

Match the Pairs

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

Left columnRight column
1fruits.includes("apple") A0 is the position of the first item, not a 'not found' result. Not found is -1, so this misses red at other positions. Use !== -1 to check it was found.
2fruits.indexOf("pear") BGives true if "apple" is in the fruits array, otherwise false
3Use includes inside an if to run code only when a value is in the array Cif (names.includes("Zoe")) { console.log("Found"); }
4Check that indexOf did not find the value by comparing the result with -1 DGives the position of "pear" in the array, or -1 if it is not found
5if (colours.indexOf("red") === 0) { console.log("Found"); } Eif (scores.indexOf(50) === -1) { console.log("Not found"); }
6let pos = fruits.includes("kiwi"); Fincludes gives true or false, not a position. To get the position of kiwi you need indexOf.

Definitions

includes(value)

Fill in the Blanks

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

1.
const pets = ["cat", "dog", "fish"]; console.log(pets.________("dog"));
2.
const names = ["Ava", "Liam", "Mia"]; console.log(names._______("Mia"));
3.
const scores = [70, 85, 92]; console.log(scores.indexOf(50) === __);
4.
const games = ["chess", "tag"]; if (games.________("tag")) console.log("Found!");
5.
const colours = ["red", "blue"]; if (_colours.includes("green")) console.log("Not found");

Spot the Error

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

Question 1
A const n = ["Ava", "Leo"];
B const i = n.indexOf("Zed");
C console.log(i);
D console.log(i === 0);
Question 2
A const n = [4, 8, 15];
B console.log(n.include(8));
C console.log(n.indexOf(8));
Question 3
A const t = ["a", "b", "c"];
B const pos = t.indexOf("b");
C if (pos === -1) {
D console.log("At", pos);
E }
Question 4
A const f = ["pie", "tea"];
B const has = f.includes("tea");
C if (has === "true") {
D console.log("Yes");
E }
Question 5
A const pets = {"cat", "dog"};
B const i = pets.indexOf("dog");
C console.log(i);

Answer Key

Searching an array with includes and indexOf
Section A: Quick Questions
Q1
A — names.includes("Ava")
Q2
B — false
Q3
A — 2
Q4
A — -1
Q5
B — if (team.indexOf("Leo") === -1)
Section B: Match the Pairs
Q1
fruits.includes("apple") → Gives true if "apple" is in the fruits array, otherwise false
Q2
fruits.indexOf("pear") → Gives the position of "pear" in the array, or -1 if it is not found
Q3
Use includes inside an if to run code only when a value is in the array → if (names.includes("Zoe")) { console.log("Found"); }
Q4
Check that indexOf did not find the value by comparing the result with -1 → if (scores.indexOf(50) === -1) { console.log("Not found"); }
Q5
if (colours.indexOf("red") === 0) { console.log("Found"); } → 0 is the position of the first item, not a 'not found' result. Not found is -1, so this misses red at other positions. Use !== -1 to check it was found.
Q6
let pos = fruits.includes("kiwi"); → includes gives true or false, not a position. To get the position of kiwi you need indexOf.
Section C: Definitions
Q1
includes(value) — Array method that gives true if the value is in the array, otherwise false. Its partner indexOf(value) gives the position, or -1 if the value is not found.
Section D: Fill in the Blanks
Q1
const pets = ["cat", "dog", "fish"]; console.log(pets.includes("dog"));
Q2
const names = ["Ava", "Liam", "Mia"]; console.log(names.indexOf("Mia"));
Q3
const scores = [70, 85, 92]; console.log(scores.indexOf(50) === -1);
Q4
const games = ["chess", "tag"]; if (games.includes("tag")) console.log("Found!");
Q5
const colours = ["red", "blue"]; if (!colours.includes("green")) console.log("Not found");
Section E: Spot the Error
Q1
D — indexOf gives -1 when the value is not found, not 0 (0 is the first position). The check should be i === -1.
Q2
B — The method is called includes, with an s on the end. include is not an array method and causes a TypeError.
Q3
C — The condition is backwards. The code inside should run when the value IS found, so it must be pos !== -1.
Q4
C — includes gives the boolean true, not the string "true", so this comparison is never true. Use if (has) {.
Q5
A — An array is written with square brackets, not curly braces. It should be const pets = ["cat", "dog"];