📖 Scenario: You are building a simple search feature for a book store's website. The website wants to count how many book titles start with a certain prefix typed by the user.
🎯 Goal: Build a TypeScript program that counts how many words in a list start with a given prefix.
📋 What You'll Learn
Create an array of strings called words with exact book titles
Create a string variable called prefix with the exact prefix to search
Use a for loop to count how many words start with prefix
Print the count as a number
💡 Why This Matters
🌍 Real World
Counting words with a prefix is useful in search engines, autocomplete features, and filtering lists in apps.
💼 Career
This skill helps in building efficient search and filtering functions in web and software development.
Progress0 / 4 steps
1
Create the list of book titles
Create an array called words with these exact strings: 'apple', 'app', 'application', 'banana', 'apricot'
DSA Typescript
Hint
Use square brackets [] to create an array and separate strings with commas.
2
Set the prefix to search
Create a string variable called prefix and set it to the exact value 'app'
DSA Typescript
Hint
Use const prefix = 'app'; to create the prefix variable.
3
Count words starting with the prefix
Create a variable called count and set it to 0. Then use a for loop with variable word to go through words. Inside the loop, use word.startsWith(prefix) to check if the word starts with prefix. If yes, increase count by 1.
DSA Typescript
Hint
Remember to initialize count before the loop and increase it inside the if block.
4
Print the count result
Use console.log(count) to print the number of words starting with the prefix.
DSA Typescript
Hint
The output should be the number 3 because three words start with 'app'.
Practice
(1/5)
1. What does the function countWordsStartingWith(words: string[], prefix: string): number do in the context of counting words with a given prefix?
easy
A. Counts the total number of characters in all words
B. Counts how many words contain the prefix anywhere inside
C. Counts how many words in the list start with the given prefix
D. Returns the longest word starting with the prefix
Solution
Step 1: Understand the function purpose
The function is designed to count words that begin with a specific prefix, not just contain it anywhere.
Step 2: Compare options with function goal
Only Counts how many words in the list start with the given prefix matches the goal of counting words starting exactly with the prefix.
Final Answer:
Counts how many words in the list start with the given prefix -> Option C
Quick Check:
Counting words starting with prefix = Counts how many words in the list start with the given prefix [OK]
Hint: Look for words starting exactly with prefix, not containing it [OK]
Common Mistakes:
Confusing 'start with' and 'contain' prefix
Thinking it returns words instead of count
Assuming it counts characters, not words
2. Which of the following TypeScript code snippets correctly counts words starting with a prefix pre in an array words?
easy
A. words.filter(word => word.startsWith(pre)).length
B. words.map(word => word.startsWith(pre)).length
C. words.reduce((count, word) => count + word.includes(pre), 0)
D. words.forEach(word => word.startsWith(pre))
Solution
Step 1: Identify correct method to filter words
The filter method returns an array of words starting with the prefix, then length counts them.
Step 2: Check other options for correctness
words.map(word => word.startsWith(pre)).length returns an array of booleans, so length is total words, not count of matches. words.reduce((count, word) => count + word.includes(pre), 0) uses includes incorrectly and sums booleans without conversion. words.forEach(word => word.startsWith(pre)) does not return a count.
Final Answer:
words.filter(word => word.startsWith(pre)).length -> Option A
Words starting with 'app' are 'apple', 'app', 'application', and 'apply' (4 words).
Step 2: Count these words
Counting them gives 4.
Final Answer:
4 -> Option B
Quick Check:
Words starting with 'app' = 4 [OK]
Hint: Count words starting exactly with prefix 'app' [OK]
Common Mistakes:
Counting 'banana' mistakenly
Counting only exact 'app' word
Confusing includes with startsWith
4. The following TypeScript code is intended to count words starting with prefix pre, but it returns 0. What is the error?
const words = ['prefix', 'preach', 'prevent', 'post'];
const pre = 'pre';
let count = 0;
for (let i = 0; i < words.length; i++) {
if (words[i].startWith(pre)) {
count++;
}
}
console.log(count);
medium
A. Method name should be startsWith, not startWith
B. Variable pre is not defined
C. Loop should start from 1, not 0
D. count should be initialized to 1
Solution
Step 1: Check method name correctness
The method to check prefix is startsWith, but code uses startWith, which is undefined.
Step 2: Understand effect of typo
Because of the typo, condition is always false, so count remains 0.
Final Answer:
Method name should be startsWith, not startWith -> Option A
Quick Check:
Correct method name fixes count [OK]
Hint: Check method spelling: startsWith, not startWith [OK]
Common Mistakes:
Using startWith instead of startsWith
Incorrect loop index start
Wrong count initialization
5. Given a large list of words, which data structure would optimize counting how many words start with a given prefix efficiently for multiple queries?
hard
A. Array sorted alphabetically
B. Stack storing words
C. Hash Map with full words as keys
D. Trie (Prefix Tree)
Solution
Step 1: Understand prefix query optimization
Trie stores words by characters in a tree, allowing fast prefix searches.
Step 2: Compare other data structures
Sorted arrays require binary search but less efficient for many prefix queries. Hash maps store full words, not prefixes. Stacks do not support prefix queries efficiently.
Final Answer:
Trie (Prefix Tree) -> Option D
Quick Check:
Trie enables fast prefix counts [OK]
Hint: Use Trie for fast prefix counting in many queries [OK]
Common Mistakes:
Choosing array or hash map for prefix queries
Not knowing Trie supports prefix efficiently
Confusing stack usage for prefix search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.