EXERCISE · AlgoCasts

Array chunking

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size

5 testsAlgoCastsGPL-3.01 alternateexercise 06 of 26

Directions

Directions

Given an array and chunk size, divide the array into many subarrays where each subarray is of length size

Examples#

JS
chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]

Try it first#

Copy the stub, make the 5 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/chunk/index.js
function chunk(array, size) {} module.exports = chunk;

See also