.includes() and .indexOf()

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Is It in There? includes

To know whether an array contains a value, use `includes()`. It answers with a boolean: `true` or `false`.

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.includes("Pyrochu")); console.log(team.includes("Zap"));

Since the result is a boolean, it fits directly in a condition:

const team = ["Aquachu", "Pyrochu", "Florachu"]; if (team.includes("Zap")) { console.log("Zap is ready to fight!"); } else { console.log("Zap is not in the team"); }

Exercise 2 β€” Where Is It? indexOf

`includes` says yes or no. `indexOf()` goes further: it gives the index where the value was found.

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.indexOf("Florachu")); console.log(team.indexOf("Aquachu"));

If the value isn't in the array, `indexOf` returns `-1`. Not `undefined`, not `0`: `-1`, a value that can never be a real index.

const team = ["Aquachu", "Pyrochu", "Florachu"]; console.log(team.indexOf("Zap"));

Exercise 3 β€” Quiz: Yes or No

What does this code print?

const moves = ["Spark", "Thunder"]; console.log(moves.includes("Charge"));
Options
β€’ false
β€’ true
β€’ -1
β€’ undefined

Hint : This method only ever answers with one of two values.

Exercise 4 β€” Ready to Fight?

Print `Pyrochu is ready!` only if Pyrochu is part of the team.

Hint : You need a method whose answer works directly as a condition.

Exercise 5 β€” Exact Match Only

Both methods look for an exact match. Uppercase and lowercase matter, and so does every single character:

const team = ["Aquachu", "Pyrochu"]; console.log(team.includes("aquachu")); console.log(team.includes("Aquachu ")); console.log(team.includes("Aquachu"));

Same for numbers: the number `5` and the string `"5"` are two different values.

const levels = [5, 8, 12]; console.log(levels.includes(5)); console.log(levels.includes("5"));

Exercise 6 β€” Quiz: Position Found

What does this code print?

const team = ["Rockachu", "Zap", "Metachu"]; console.log(team.indexOf("Metachu"));
Options
β€’ 2
β€’ 3
β€’ true
β€’ -1

Hint : The answer is a position, counted the way arrays always count.

Exercise 7 β€” Unknown Move

Find the position of the chosen move. If it isn't known, print `Unknown move`, otherwise print its position.

Hint : You need a position, and a special value tells you when nothing was found.

Exercise 8 β€” includes vs indexOf

Match each expression with its result.

Items to match
β€’ includes(x)
β€’ indexOf(x)
β€’ indexOf(x) when x is missing
β€’ ["Zap"].includes("zap")
Definitions
β€’ true or false
β€’ The index of x
β€’ -1
β€’ false

Exercise 9 β€” Quiz: Which Method?

You want to know at which position `"Elixir"` sits in `bag`. Which line do you need?

Options
β€’ bag.indexOf("Elixir")
β€’ bag.includes("Elixir")
β€’ bag["Elixir"]
β€’ bag.length("Elixir")

Hint : One method answers a yes/no question, the other answers a where question.

Exercise 10 β€” Move Check

Zap wants to use the moves in the variable`chosen`. 1. If the move is in `moves`, print `"Zap uses X! (move number N)"` where N is its position counted from 1 2. Otherwise, print `"Zap doesn't know X"` Test your code with a move that exists and one that doesn't.

Starter code
const moves = ["Spark", "Thunder", "Charge"]; let chosen = "Thunder"; // Write your code here
Back to the course