Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save anonymous/3d5680bf5f02c3e46051 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/3d5680bf5f02c3e46051 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/mushfick 's solution for Bonfire: Chunky Monkey
// Bonfire: Chunky Monkey
// Author: @mushfick
// 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%20count%20%3D%201%3B%0A%20%20var%20tempArray%20%3D%20%5B%5D%3B%0A%20%20var%20finalArray%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%20arr.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20if(%20count%20%3C%3D%20size)%20%7B%0A%20%20%20%20%20%20tempArray.push(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20finalArray.push(tempArray)%3B%0A%20%20%20%20%20%20tempArray%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20count%20%3D%201%3B%0A%20%20%20%20%20%20tempArray.push(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%20%20%20count%20%2B%3D%201%3B%0A%20%20%7D%0A%20%20finalArray.push(tempArray)%3B%0A%20%20return%20finalArray%3B%0A%7D%0A%0Achunk(%5B0%2C%201%2C%202%2C%203%2C%204%2C%205%5D%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function chunk(arr, size) {
// Break it up.
var count = 1;
var tempArray = [];
var finalArray = [];
for(var i = 0; i < arr.length; i++) {
if( count <= size) {
tempArray.push(arr[i]);
}
else {
finalArray.push(tempArray);
tempArray = [];
count = 1;
tempArray.push(arr[i]);
}
count += 1;
}
finalArray.push(tempArray);
return finalArray;
}
chunk([0, 1, 2, 3, 4, 5], 2);
@mushfick
Copy link

Horrible way of doing this. Check the other gist for a better and much more cleaner solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment