Skip to content

Instantly share code, notes, and snippets.

Created August 15, 2017 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9969221c11740be81618c08ca7c9ab50 to your computer and use it in GitHub Desktop.
Save anonymous/9969221c11740be81618c08ca7c9ab50 to your computer and use it in GitHub Desktop.
ChunkeyMonkeyV1.01
function chunkArrayInGroups(arr, size) {
/* input: array of letters, size of new array
return: array chunked into groups based on size,
return the last array of elements if group cannot be built from the remaining elements to match size
*/
var builtArr = [];
var returnArr = [];
for (var i= 0; i < arr.length; i++){
if (i%size === 0 && (i!== 0)){
returnArr.push(builtArr);
builtArr = [];
}
builtArr.push(arr[i]);
}
returnArr.push(builtArr);
return returnArr;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment