Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save anonymous/32f9769eb1fb04bb37df to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/32f9769eb1fb04bb37df to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/srkama 's solution for Bonfire: Chunky Monkey
// Bonfire: Chunky Monkey
// Author: @srkama
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20resultArray%20%3D%20%5B%5D%3B%0A%20%20flag%3Dtrue%3B%0A%20%20start%3D0%3B%0A%20%20end%3Dsize%3B%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20while(flag)%20%7B%0A%20%20%20%20%20a%20%3D%20arr.slice(start%2Cend)%3B%0A%20%20%20%20%20if(a.length)%20%7B%0A%20%20%20%20%20%20%20resultArray.push(a)%3B%0A%20%20%20%20%20%20%20start%3Dend%3B%0A%20%20%20%20%20%20%20end%3Dend%2Bsize%3B%0A%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20flag%3Dfalse%3B%0A%20%20%20%20%20%7D%0A%20%20%7D%0A%20%20return%20resultArray%3B%0A%7D%0A%0Achunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function chunk(arr, size) {
resultArray = [];
flag=true;
start=0;
end=size;
// Break it up.
while(flag) {
a = arr.slice(start,end);
if(a.length) {
resultArray.push(a);
start=end;
end=end+size;
} else {
flag=false;
}
}
return resultArray;
}
chunk(["a", "b", "c", "d"], 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment