Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Created June 21, 2016 14:40
Show Gist options
  • Save brainyfarm/055c755895fa2d32f52f80716a36130e to your computer and use it in GitHub Desktop.
Save brainyfarm/055c755895fa2d32f52f80716a36130e to your computer and use it in GitHub Desktop.
Olawale/FreeCodeCamp Algorithm: Chunk Array in Groups
function chunkArrayInGroups(arr, size) {
// A variable to keep the chunked arrays
var holder = [];
// While the length of the arr is greater than 0
while(arr.length > 0){
// Splice from index 0 to size and push the spliced item into the holder array
// arr.splice() mutates the arr
holder.push(arr.splice(0, size));
} // End of while loop
// Return the holder arr
return holder;
}
chunkArrayInGroups(["a", "b", "c", "d" , "e"], 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment