0
0
DSA Javascriptprogramming~15 mins

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

Choose your learning style9 modes available
Count Words with Given Prefix
📖 Scenario: You are working on a simple text analysis tool. You want to find out how many words in a list start with a certain prefix. This can help in searching or filtering words quickly.
🎯 Goal: Build a small program that counts how many words in a given list start with a specific prefix.
📋 What You'll Learn
Create an array of words with exact values
Create a variable to hold the prefix string
Use a loop to count how many words start with the prefix
Print the count as the final output
💡 Why This Matters
🌍 Real World
Counting words with a prefix is useful in search engines, autocomplete features, and filtering lists quickly.
💼 Career
This skill helps in text processing tasks common in software development, data analysis, and building user-friendly search tools.
Progress0 / 4 steps
1
Create the list of words
Create an array called words with these exact strings: "apple", "app", "application", "banana", "apricot"
DSA Javascript
Hint

Use square brackets [] to create an array and put the words inside quotes separated by commas.

2
Set the prefix to search
Create a variable called prefix and set it to the string "app"
DSA Javascript
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, if word.startsWith(prefix) is true, increase count by 1.
DSA Javascript
Hint

Use startsWith method to check the prefix and increase count inside the loop.

4
Print the count
Use console.log(count) to print the number of words starting with the prefix.
DSA Javascript
Hint

Use console.log(count) to show the result.