Workshop › Text detective › Step 7 of 13

Count each word

What we are doing

Which words repeat? A Map keeps a count for each word: we go through the words and add one to that word's count.

```js
const counts = new Map()
for (const word of words) {
  const key = word.toLowerCase()
  counts.set(key, (counts.get(key) ?? 0) + 1)
}

+ '\nDifferent words: ' + counts.size
```

What it means

Your turn

  1. Under the words line write the counts map and the for loop.
  2. Add + '\nDifferent words: ' + counts.size at the end of the report line.

Skills: Loops, Arrays and objects

All steps

  1. Read the text
  2. Count the characters
  3. Update as you type
  4. Split into words
  5. The extra-space trap
  6. An empty box has no words
  7. Count each word
  8. Drop the punctuation
  9. The most common word
  10. Shift a letter
  11. Encode a whole text
  12. The secret line
  13. Build it yourself: crack the code