0
0
DSA Javascriptprogramming~10 mins

Count Words with Given Prefix in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a word starts with the given prefix.

DSA Javascript
function startsWith(word, prefix) {
  return word.[1](prefix);
}
Drag options to blanks, or click blank then click option'
Aincludes
BstartsWith
CendsWith
DindexOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' which checks anywhere in the word, not just the start.
Using 'endsWith' which checks the end of the word.
2fill in blank
medium

Complete the code to count how many words start with the prefix.

DSA Javascript
function countWords(words, prefix) {
  let count = 0;
  for (const word of words) {
    if (word.[1](prefix)) {
      count++;
    }
  }
  return count;
}
Drag options to blanks, or click blank then click option'
AstartsWith
BindexOf
CendsWith
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' which counts words containing prefix anywhere.
Using 'endsWith' which checks the end of the word.
3fill in blank
hard

Fix the error in the code to correctly count words with the prefix.

DSA Javascript
function countWords(words, prefix) {
  return words.filter(word => word.[1](prefix)).length;
}
Drag options to blanks, or click blank then click option'
AendsWith
Bincludes
CstartsWith
DindexOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' which matches anywhere in the word.
Using 'endsWith' which checks the end of the word.
4fill in blank
hard

Fill both blanks to create a function that returns a list of words starting with the prefix.

DSA Javascript
function filterWords(words, prefix) {
  return words.[1](word => word.[2](prefix));
}
Drag options to blanks, or click blank then click option'
Afilter
Bmap
CstartsWith
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'filter' which changes the array instead of selecting.
Using 'includes' which matches prefix anywhere, not just start.
5fill in blank
hard

Fill all three blanks to create a function that returns the count of words starting with the prefix using reduce.

DSA Javascript
function countWordsReduce(words, prefix) {
  return words.reduce((count, word) => {
    return count + (word.[1](prefix) ? [2] : [3]);
  }, 0);
}
Drag options to blanks, or click blank then click option'
AstartsWith
B1
C0
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' instead of 'startsWith'.
Adding wrong values instead of 1 or 0.