Chunk Array

EasyArrays10 min4 tests

Implement a chunk function that splits an array into groups of a given size.

Requirements

  • Split the array into sub-arrays of the specified size
  • The last chunk may have fewer elements if the array doesn't divide evenly
  • Return an empty array if the input is empty

Example

chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
chunk([1, 2, 3], 1);       // [[1], [2], [3]]
Hints (3)
  1. Use a for loop that increments by the chunk size.
  2. Use Array.prototype.slice to extract each chunk.
  3. Push each slice into a result array.

Topics

  • Arrays

Asked at

Amazon · Shopify · Atlassian

Join Us
blur