Skip to content

Instantly share code, notes, and snippets.

  • Save anonymous/8e18c301a1ff4816e82b to your computer and use it in GitHub Desktop.
Save anonymous/8e18c301a1ff4816e82b to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/elliotfriend 's solution for Bonfire: Chunky Monkey
// Bonfire: Chunky Monkey
// Author: @elliotfriend
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20var%20multiArray%20%3D%20%5B%5D%3B%0A%20%20var%20tempArray%20%3D%20%5B%5D%3B%0A%20%20while%20(arr.length%20%3E%200)%20%7B%0A%20%20%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20size%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20if%20(arr.length%20%3E%200)%20%7B%0A%20%20%20%20%20%20%20%20tempArray.push(arr%5B0%5D)%3B%0A%20%20%20%20%20%20%20%20arr.shift()%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20multiArray.push(tempArray)%3B%0A%20%20%20%20tempArray%20%3D%20%5B%5D%3B%0A%20%20%7D%0A%20%20return%20multiArray%3B%0A%7D%0A%0A%2F%2Fchunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202)%3B%0Achunk(%5B0%2C%201%2C%202%2C%203%2C%204%2C%205%5D%2C%204)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function chunk(arr, size) {
// Break it up.
var multiArray = [];
var tempArray = [];
while (arr.length > 0) {
for (var i = 0; i < size; i++) {
if (arr.length > 0) {
tempArray.push(arr[0]);
arr.shift();
}
}
multiArray.push(tempArray);
tempArray = [];
}
return multiArray;
}
//chunk(["a", "b", "c", "d"], 2);
chunk([0, 1, 2, 3, 4, 5], 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment