Created
March 27, 2018 19:35
-
-
Save eugenserbanescu/ec93c5d893ed204b3f61e7cd32e1dd99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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