-
-
Save anonymous/9969221c11740be81618c08ca7c9ab50 to your computer and use it in GitHub Desktop.
ChunkeyMonkeyV1.01
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
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