Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Group By Length

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 156: Data Transformations: map, filter, reduce

Implement `groupByLength(words)` that groups an array of strings by their character length. Return an object where each key is a length and each value is an array of words with that length.

Sample tests

Input: groupByLength(["cat","dog","elephant","ox"])
Output: {"2":["ox"],"3":["cat","dog"],"8":["elephant"]}
Input: groupByLength([])
Output: {}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting to initialize the group array before pushing.

Learning resources

  • MDN: Array.prototype.reduce
Approach & explanation (try first)

Bucketing each string by its length produces an object mapping length to the matching words.

Loading...
⌘/Ctrl + Enter

Run your code to see results.