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)
- Use a for loop that increments by the chunk size.
- Use
Array.prototype.sliceto extract each chunk. - Push each slice into a result array.
Topics
- Arrays
Asked at
Amazon · Shopify · Atlassian
