Command Palette

Search for a command to run...

Hectal
Phase 14: Advanced Language
JavaScript · Exercise 3 of 5 PreviousNext
Intermediate

Word counts with a Map

Write countWords(text) that returns a Map from each word (lower-case, without punctuation) to how many times it appears, in the order words first appear.

Write topWords(text, n) that returns the n most common words as [word, count] pairs, most common first; words with equal counts keep their first-appearance order.

When the Count button is clicked, show the top 3 words from the textarea in #top, one <li> each, written like red: 3.

What Check tests

  1. countWords returns a Map of counts
  2. Punctuation and capitals are ignored
  3. topWords returns the most common pairs first
  4. Ties keep first-appearance order
  5. Clicking Count shows the top 3
  6. No errors

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.