0
0
DSA Javascriptprogramming~10 mins

Count Words with Given Prefix in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Count Words with Given Prefix
Start with word list
Check each word
Does word start with prefix?
NoSkip word
Yes
Increment count
Repeat for all words
Return count
We check each word one by one to see if it starts with the prefix. If yes, we add to count. Finally, we return the total count.
Execution Sample
DSA Javascript
function countPrefix(words, prefix) {
  let count = 0;
  for (const word of words) {
    if (word.startsWith(prefix)) count++;
  }
  return count;
}
This code counts how many words in the list start with the given prefix.
Execution Table
StepOperationCurrent WordPrefix CheckCountVisual State
1Initialize count--0count=0
2Check wordappleapple.startsWith('ap') = true1count=1
3Check wordappapp.startsWith('ap') = true2count=2
4Check wordbananabanana.startsWith('ap') = false2count=2
5Check wordapricotapricot.startsWith('ap') = true3count=3
6Check wordbatbat.startsWith('ap') = false3count=3
7End loop--3Final count=3
💡 All words checked, loop ends and count returned
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
count01223333
current word-appleappbananaapricotbat--
Key Moments - 3 Insights
Why does count only increase when prefix check is true?
Because we only want to count words that start with the prefix. See execution_table rows 2,3,5 where prefix check is true and count increases.
What happens if no words start with the prefix?
Count stays zero because the condition word.startsWith(prefix) is never true. This is shown in execution_table rows where prefix check is false and count remains unchanged.
Why do we check each word individually instead of all at once?
Because each word must be tested separately to see if it starts with the prefix. The loop in execution_table rows 2-6 shows this step-by-step checking.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the count after checking the word 'app'?
A1
B2
C3
D0
💡 Hint
Check the 'Count' column at Step 3 in execution_table
At which step does the prefix check return false for the first time?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Prefix Check' column in execution_table rows
If the prefix was changed to 'ba', what would be the count after checking all words?
A1
B3
C2
D0
💡 Hint
Look at words starting with 'ba' in the variable_tracker and execution_table
Concept Snapshot
Count Words with Given Prefix:
- Loop through each word in list
- Check if word starts with prefix
- If yes, increase count by 1
- Return total count after loop
- Uses string.startsWith(prefix) method
Full Transcript
This concept shows how to count words that start with a given prefix. We start with a count of zero. Then for each word, we check if it starts with the prefix using the startsWith method. If yes, we add one to the count. We repeat this for all words. Finally, we return the count. The execution table shows each step with the current word, prefix check result, and count. The variable tracker shows how count and current word change over time. Key moments clarify why count only increases on true prefix checks, what happens if no words match, and why we check words one by one. The quiz tests understanding of count values and prefix checks at different steps.