Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Tally with Reduce

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `tally(words)` returning an object mapping each word to its count, built with `reduce`.

Sample tests

Input: tally(["a","b","a"])
Output: {"a":2,"b":1}
Input: tally([])
Output: {}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting to return the accumulator from the reducer callback.

Learning resources

  • MDN: Array.prototype.reduce
Visualize this concept: Higher-Order Functions →
Approach & explanation (try first)

reduce builds the tally object by incrementing each word's count, returning the accumulator each step.

Loading...
⌘/Ctrl + Enter

Run your code to see results.