Command Palette

Search for a command to run...

Hectal
Phase 5: Arrays
JavaScript · Exercise 5 of 6 PreviousNext
Intermediate

The most common words

Write topWords(text, n) that returns an array of the n most common words in text, most common first.

Words are separated by spaces (there may be several spaces in a row, or spaces at the start and end). Ignore capital letters: Go, go and GO are the same word, returned in small letters.

When two words appear the same number of times, put them in A-to-Z order. If there are fewer than n different words, return them all.

Example: topWords("the cat and the dog and the bird", 2) returns ["the", "and"].

What Check tests

  1. The example gives ["the", "and"]
  2. Ties are in A-to-Z order
  3. Capital letters are ignored
  4. Extra spaces don't create empty words
  5. Asking for 0 words gives []

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.