📖 Scenario: Imagine you are building a simple search feature for a small library website. Users can type a word or phrase, and you want to find all the books whose titles contain that word or phrase.
🎯 Goal: Build a program that searches for a given substring inside a list of book titles and returns all titles that contain that substring.
📋 What You'll Learn
Create an array called bookTitles with exact book title strings
Create a variable called searchTerm with the exact substring to search
Use a for loop with variable title to iterate over bookTitles
Use the includes() method to check if title contains searchTerm
Collect matching titles in an array called matchingTitles
Print the matchingTitles array at the end
💡 Why This Matters
🌍 Real World
Searching for substrings in text is common in search engines, filtering lists, and autocomplete features on websites and apps.
💼 Career
Understanding substring search helps in building search functionalities, text processing, and data filtering in software development roles.
Progress0 / 4 steps
1
Create the list of book titles
Create an array called bookTitles with these exact strings: 'The Great Gatsby', 'Great Expectations', 'The Grapes of Wrath', 'Gone with the Wind', 'The Wind in the Willows'
DSA Javascript
Hint
Use square brackets [] to create an array and separate strings with commas.
2
Set the search term
Create a variable called searchTerm and set it to the string 'Great'
DSA Javascript
Hint
Use const to create a variable and assign the string 'Great'.
3
Find matching titles
Create an empty array called matchingTitles. Use a for loop with variable title to iterate over bookTitles. Inside the loop, use if (title.includes(searchTerm)) to check if title contains searchTerm. If yes, add title to matchingTitles using matchingTitles.push(title).
DSA Javascript
Hint
Remember to create an empty array before the loop. Use for (const title of bookTitles) to loop. Use includes() to check substring.
4
Print the matching titles
Write a console.log statement to print the matchingTitles array.
DSA Javascript
Hint
Use console.log(matchingTitles) to display the array of matching titles.
Practice
(1/5)
1. What does the indexOf method do when used on a string in JavaScript?
easy
A. Converts the string to uppercase
B. Replaces a substring with another string
C. Splits the string into an array of substrings
D. Finds the position of the first occurrence of a substring
Solution
Step 1: Understand the purpose of indexOf
The indexOf method searches a string for a specified substring and returns the position of its first occurrence.
Step 2: Confirm behavior on strings
If the substring is found, it returns the zero-based index; if not found, it returns -1.
Final Answer:
Finds the position of the first occurrence of a substring -> Option D
Quick Check:
indexOf = position of substring [OK]
Hint: Remember: indexOf returns position or -1 if not found [OK]
Common Mistakes:
Confusing indexOf with replace method
Thinking indexOf splits the string
Assuming indexOf changes the string case
2. Which of the following is the correct syntax to check if the substring 'cat' exists in the string text using includes method?
easy
A. text.include('cat')
B. includes(text, 'cat')
C. text.includes('cat')
D. text.contains('cat')
Solution
Step 1: Recall the correct method name
The correct method to check substring presence is includes, called on the string object.
Step 2: Verify syntax
The syntax is string.includes(substring), so text.includes('cat') is correct.