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
- The example gives
["the", "and"] - Ties are in A-to-Z order
- Capital letters are ignored
- Extra spaces don't create empty words
- 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.