Created
June 21, 2016 14:40
-
-
Save brainyfarm/055c755895fa2d32f52f80716a36130e to your computer and use it in GitHub Desktop.
Olawale/FreeCodeCamp Algorithm: Chunk Array in Groups
This file contains hidden or 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
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