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?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.const pets = ["cat", "dog", "fish"];
console.log(pets.includes("dog"));const names = ["Ava", "Liam", "Mia"];
console.log(names.indexOf("Mia"));const scores = [70, 85, 92];
console.log(scores.indexOf(50) === -1);const games = ["chess", "tag"];
if (games.includes("tag"))
console.log("Found!");const colours = ["red", "blue"];
if (!colours.includes("green"))
console.log("Not found");