0
0
DSA Typescriptprogramming~15 mins

Count Words with Given Prefix in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Count Words with Given Prefix
📖 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'.