0
0
DSA Typescriptprogramming~10 mins

Count Words with Given Prefix in DSA Typescript - 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 Typescript
function startsWith(word: string, prefix: string): boolean {
  return word.[1](prefix);
}
Drag options to blanks, or click blank then click option'
AindexOf
Bincludes
CendsWith
DstartsWith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' checks anywhere in the string, not just the start.
2fill in blank
medium

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

DSA Typescript
function countWords(words: string[], prefix: string): number {
  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'
Aincludes
BstartsWith
CendsWith
DindexOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' counts words that have the prefix anywhere, not just at the start.
3fill in blank
hard

Fix the error in the function to return the correct count of words starting with prefix.

DSA Typescript
function countWords(words: string[], prefix: string): number {
  let count = 0;
  for (const word of words) {
    if (word.[1](prefix)) {
      count += 1;
    }
  }
  return count;
}
Drag options to blanks, or click blank then click option'
AstartsWith
Bincludes
CendsWith
Dsubstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' or 'endsWith' instead of 'startsWith'.
4fill in blank
hard

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

DSA Typescript
function filterWords(words: string[], prefix: string): string[] {
  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' changes the array instead of selecting items.
5fill in blank
hard

Fill all three blanks to create a function that returns a dictionary with counts of words starting with each prefix in prefixes array.

DSA Typescript
function countWordsByPrefixes(words: string[], prefixes: string[]): {[key: string]: number} {
  const result: {[key: string]: number} = {};
  for (const prefix of prefixes) {
    result[[1]] = words.filter(word => word.[2](prefix)).[3];
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Aprefix
BstartsWith
Clength
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'includes' instead of 'startsWith' causes wrong counts.