📖 Scenario: You are building a simple text search tool that helps users find if a small word or phrase exists inside a larger text. This is like looking for a word in a book or a message.
🎯 Goal: Build a program that checks if a given pattern string exists inside a text string using substring search.
📋 What You'll Learn
Create a string variable called text with the exact value 'hello world'.
Create a string variable called pattern with the exact value 'world'.
Use a method to check if pattern is found inside text.
Print 'Pattern found' if the pattern exists in the text, otherwise print 'Pattern not found'.
💡 Why This Matters
🌍 Real World
Searching for words or phrases inside documents, messages, or web pages is a common task in text editors, search engines, and chat applications.
💼 Career
Understanding substring search is fundamental for roles in software development, especially when working with text processing, data validation, and user input handling.
Progress0 / 4 steps
1
Create the text string
Create a string variable called text and set it to the exact value 'hello world'.
DSA Typescript
Hint
Use const to create a string variable named text with the value 'hello world'.
2
Create the pattern string
Create a string variable called pattern and set it to the exact value 'world'.
DSA Typescript
Hint
Use const to create a string variable named pattern with the value 'world'.
3
Check if pattern exists in text
Use the includes method on text to check if pattern exists inside it. Store the result in a boolean variable called found.
DSA Typescript
Hint
Use text.includes(pattern) to check if pattern is inside text.
4
Print the search result
Write an if statement that prints 'Pattern found' if found is true, otherwise print 'Pattern not found'.
DSA Typescript
Hint
Use if (found) { console.log('Pattern found'); } else { console.log('Pattern not found'); } to print the result.
Practice
(1/5)
1. What is the main goal of substring search algorithms in strings?
easy
A. To sort all characters in a string alphabetically
B. To find if a smaller string exists inside a larger string
C. To reverse the entire string
D. To count the total number of vowels in a string
Solution
Step 1: Understand substring search purpose
Substring search algorithms check if a smaller string (pattern) appears inside a bigger string (text).
Step 2: Compare options with substring search definition
Only To find if a smaller string exists inside a larger string matches this goal. Sorting, reversing, or counting vowels are different tasks.
Final Answer:
To find if a smaller string exists inside a larger string -> Option B
Hint: Substring search means finding a smaller string inside a bigger one [OK]
Common Mistakes:
Confusing substring search with sorting or reversing strings
Thinking substring search counts characters
Mixing substring search with string length calculation
2. Which of the following is the correct TypeScript syntax to check if string text contains substring pattern?
easy
A. text.includes(pattern)
B. text.contains(pattern)
C. text.has(pattern)
D. text.find(pattern)
Solution
Step 1: Recall TypeScript string methods
TypeScript strings have a method called includes to check substring presence.
Step 2: Verify method names
Only includes is valid. contains, has, and find are not string methods in TypeScript.
Final Answer:
text.includes(pattern) -> Option A
Quick Check:
Use includes() to check substring presence [OK]
Hint: Use includes() method to check substring in TypeScript [OK]
Common Mistakes:
Using non-existent methods like contains() or has()
Confusing includes() with find() which is for arrays
Forgetting parentheses after method name
3. What will be the output of the following TypeScript code?
const text = "hello world";
const pattern = "wor";
console.log(text.indexOf(pattern));
medium
A. 6
B. -1
C. 3
D. 0
Solution
Step 1: Understand indexOf method
indexOf returns the starting index of the substring if found, else -1.
Step 2: Find substring position in text
In "hello world", substring "wor" starts at index 6 (0-based counting: h=0, e=1, l=2, l=3, o=4, space=5, w=6).
Final Answer:
6 -> Option A
Quick Check:
indexOf("wor") = 6 [OK]
Hint: indexOf returns start index or -1 if not found [OK]
Common Mistakes:
Counting index starting from 1 instead of 0
Confusing indexOf with includes (which returns boolean)
Assuming indexOf returns substring length
4. Identify the error in the following TypeScript code snippet for substring search:
const text = "typescript";
const pattern = "script";
if (text.find(pattern)) {
console.log("Found");
} else {
console.log("Not Found");
}
medium
A. Using if statement without parentheses
B. Missing semicolon after console.log statements
C. Pattern variable is not declared
D. Using find() method on string which does not exist
Solution
Step 1: Check string methods in TypeScript
Strings do not have a find() method. The correct method to check substring is includes() or indexOf().
Step 2: Verify other code parts
Semicolons are optional in TypeScript, pattern is declared, and if statement syntax is correct.
Final Answer:
Using find() method on string which does not exist -> Option D
Quick Check:
Strings have includes(), not find() [OK]
Hint: Use includes() or indexOf(), not find() on strings [OK]
Common Mistakes:
Assuming find() works on strings like arrays
Thinking semicolons are mandatory causing errors
Misreading if statement syntax
5. You want to find all starting indices of substring pattern in string text. Which approach correctly finds multiple occurrences in TypeScript?
hard
A. Use text.indexOf(pattern) once and return that single index
B. Use text.includes(pattern) repeatedly without changing index
C. Use a loop with indexOf(pattern, startIndex) updating startIndex after each find
D. Use text.findAll(pattern) method to get all indices
Solution
Step 1: Understand multiple substring search
To find all occurrences, you must repeatedly search starting after the last found index.
Step 2: Analyze each option
Use a loop with indexOf(pattern, startIndex) updating startIndex after each find uses a loop with indexOf and updates start index, which is correct. Use text.includes(pattern) repeatedly without changing index does not update index, so it loops infinitely or finds only once. Use text.indexOf(pattern) once and return that single index finds only one occurrence. Use text.findAll(pattern) method to get all indices uses a non-existent method.
Final Answer:
Use a loop with indexOf(pattern, startIndex) updating startIndex after each find -> Option C
Quick Check:
Loop with indexOf and updated start index finds all [OK]
Hint: Loop with indexOf and update start index to find all [OK]
Common Mistakes:
Not updating start index causing infinite loops
Using includes() which returns boolean, not index
Expecting findAll() method which does not exist
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.