Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active April 5, 2016 06:18
Show Gist options
  • Select an option

  • Save mmloveaa/998c73509badc7a6c13ff15474812162 to your computer and use it in GitHub Desktop.

Select an option

Save mmloveaa/998c73509badc7a6c13ff15474812162 to your computer and use it in GitHub Desktop.
4-4 FCC Q10 Chunky Monkey
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
Array.push()
Array.slice()
function chunkArrayInGroups(arr, size) {
// Break it up.
var arr1=[];
for(var i=0; i<arr.length; i+=size) {
// console.log(arr.slice(i,size))
arr1.push(arr.slice(i,i+size))
}
return arr1;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
chunkArrayInGroups(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]].
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) should return [[0, 1, 2], [3, 4, 5]].
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) should return [[0, 1], [2, 3], [4, 5]].
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]].
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]].
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment