EXERCISE · AlgoCasts
Array chunking
Given an array and chunk size, divide the array into many subarrays where each subarray is of length size
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;Solution#
JScompleted_exercises/chunk/index.js
function chunk(array, size) { const chunked = []; let index = 0; while (index < array.length) { chunked.push(array.slice(index, index + size)); index += size; } return chunked;} module.exports = chunk;function chunk(array, size) { const chunked = []; for (let element of array) { const last = chunked[chunked.length - 1]; if (!last || last.length === size) { chunked.push([element]); } else { last.push(element); } } return chunked;}const chunk = require('./index'); test('function chunk exists', () => { expect(typeof chunk).toEqual('function');}); test('chunk divides an array of 10 elements with chunk size 2', () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; const chunked = chunk(arr, 2); expect(chunked).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]);}); test('chunk divides an array of 3 elements with chunk size 1', () => { const arr = [1, 2, 3]; const chunked = chunk(arr, 1); expect(chunked).toEqual([[1], [2], [3]]);}); test('chunk divides an array of 5 elements with chunk size 3', () => { const arr = [1, 2, 3, 4, 5]; const chunked = chunk(arr, 3); expect(chunked).toEqual([[1, 2, 3], [4, 5]]);}); test('chunk divides an array of 13 elements with chunk size 5', () => { const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; const chunked = chunk(arr, 5); expect(chunked).toEqual([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13]]);}); Verbatim from the repo at 30cf7634. Alternate tabs are the commented-out solutions with the leading // removed and nothing else changed. Run the tests locally with npx jest chunk inside the fork.
See also