Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eugenserbanescu/ec93c5d893ed204b3f61e7cd32e1dd99 to your computer and use it in GitHub Desktop.
Save eugenserbanescu/ec93c5d893ed204b3f61e7cd32e1dd99 to your computer and use it in GitHub Desktop.
const SAMPLE_DATA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
function divideInto(data, chunkSize) {
// make the container arrays
const containers = [];
for (let i = 0; i < chunkSize; i++) {
containers[i] = [];
}
// push bits of data into each of the arrays;
data.forEach((item, index) => {
const containerIndex = (index + chunkSize) % chunkSize;
containers[containerIndex].push(item);
});
return containers;
}
// console.log(divideInto(SAMPLE_DATA, 3));
// console.log(divideInto(SAMPLE_DATA, 5));
// console.log(divideInto(SAMPLE_DATA, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment