BeginnerCoder

Lesson 14 — Group with reduce

reduce can build an object

reduce is not only for sums. Start it with an empty object {} and it can count or group things. For each word, add 1 to its counter; (acc[w] || 0) means "the current count, or 0 if this word is new".

acc["a"] = (acc["a"] || 0) + 1;

Why it matters: counting and grouping power every chart, tag cloud and report.

Want it another way?
Task

Inside reduce, add 1 to acc[w] (starting from 0), then log each word count.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.