Skip to content

Instantly share code, notes, and snippets.

@piqueen314
Created December 11, 2015 06:37
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 piqueen314/3ea0a5ba2d70f26a76c5 to your computer and use it in GitHub Desktop.
Save piqueen314/3ea0a5ba2d70f26a76c5 to your computer and use it in GitHub Desktop.
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.
// Bonfire: Chunky Monkey
// Author: @piqueen314
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function chunk(arr, size) {
var newArr = [];
for (var i=0; i < arr.length; i+=size)
newArr.push(arr.slice(i,i+size));
return newArr;
}
chunk(['a', 'b', 'c', 'd'], 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment