String Search Methods, online
Six activities your students do on their own devices. See who's finished, and their scores.
String Search Methods in one minute
What students need to know
indexOf() returns -1 when the substring doesn't appear. This tests whether "cat" appears anywhere inside word.
includes() checks for a substring and returns a boolean result. indexOf() returns a position number, not true or false.
Mistakes students make
- 'includes' must be lowercase - a capital I causes an error.
- Method names are camelCase - it should be indexOf, not indexof.
- Missing parentheses and the search value - includes() needs an argument.
- Missing parentheses and the search value - indexOf() needs an argument.
What students will learn in this module
Every question in Module 31
Quick questions
- Does includes() return true or false?AYesBNo
- Does indexOf() return a boolean like includes() does?AYesBNo
- Does indexOf() return -1 when the substring isn't found?AYesBNo
- Is 'indexof' with a lowercase o valid JavaScript syntax?AYesBNo
- Do these search methods need a substring passed as an argument?AYesBNo
Key terms
Students type the meaning of:
includes()
Match the pairs
text.includes("world")Match it to what it doestext.indexOf("world")Match it to what it doesThe value returned when a substring isn't foundMatch it to what it doesChecks whether a substring exists in a stringMatch it to what it doestext.Includes("world")Match it to what it doestext.indexof("world")Match it to what it doesFill in the blanks
console.log(text.________("world"));Hint: Type the method that checks if a string contains a substring.
console.log(text._______("world"));Hint: Type the method that returns the position of a substring.
let found = word.________("cat");Hint: Type the method that checks if a string contains a substring.
let pos = word._______("cat");Hint: Type the method that returns the position of a substring.
let hasDog = text.________("dog");Hint: Type the method that checks if a string contains a substring.
Spot the error
Question 1
Alet a = "hi there";Bconsole.log(a.Includes("hi"));Cconsole.log(a);
Question 2
Alet b = "good day";Bconsole.log(b);Cconsole.log(b.indexof("day"));
Question 3
Alet c = "hi there";Bconsole.log(c);Cconsole.log(c.includes);
Question 4
Aconsole.log(d.indexOf);Blet d = "good day";Cconsole.log(d);
Question 5
Alet e = "hi there";Bconsole.log(e.includes("hi"));Cconsole.log(e);Dconsole.log(e.Includes("hi"));
