Chunky Monkey Help

Chunky Monkey Hell: Ok I ve been doing this for hours, and I just can’t get it. Please guide me in the right direction

function chunkArrayInGroups(arr, size) {
// Break it up.
var arraySize = arr.length;
var splitArray = [];
var tempArray = [];
var i = 0;
while(i < arraySize + 1){
tempArray = arr.slice(i,size);
i += size;
size += size;
}//end while

return splitArray;


chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);

Hm, I’ll have to find where I put my solution code…

A few things I noticed that will hopefully help you:
-Why do you want to go to arraySize+1? indexes start at zero so arr[arr.length] will give an error if that makes sense.

-Why are you changing size? If the size that gets passed in is 2, you want all of your arrays within the main array to have a length of two. Instead of changing size, how about creating variables to keep track of the front and back index that you are trying to slice. Another option could be using splice which will actually modify the array for you.

-You might need to add a check in case the length of the array is not perfectly divisible by size and you don’t want to end up with an index out of bounds error.

Hopefully this was helpful without giving too much away. Good Luck!

Yes, I think along the lines of what @twmilli is saying, it’s possible for i += size to result in an i value that exits the while loop prematurely (i.e. before it finishes completely creating the new array).

Additionally, the size variable starts incrementing too much. For instance, if the initial size is 3 then on the first iteration size will be incremented by 3 to 6. But on the next iteration size is now 6, so size will be incremented by 6 and then become 12. This is also a problem because i is incremented by size, so i would be 0, then 3, then 9.

It would also be useful to mention that assigning variables assigns a reference to a value in Javascript.

So in this example if you set x = size, then x is actually a reference to the ‘size’ variable’s value, and not a copy of it. I hope I explained that correctly.

I was able to work with your code based on this considerations to get a passable solution. I hope this helps good luck!

Ok all,

Here’s what I ended up with. I just couldn’t stop till I got it.

function chunkArrayInGroups(arr, size) {
// Break it up.
var currentPoint = 0;
var endPoint = arr.length;
var tempArray = [];
var returnArray = [];

while(currentPoint < endPoint){
for(var i = 0; i < size; i++){
if(typeof(arr[currentPoint]) != “undefined”){
tempArray[i] = arr[currentPoint];
currentPoint ++;
}//end if
}//end for
tempArray = [];

}//end while

return returnArray;

}//end chunkArrayInGroups

chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);

1 Like

Nice work on figuring it out!

Congratulations! Now all that’s left is check what happens if the user types 0 in the function, it will hang up on you (infinite loop). You can return the function if the user types 0 at the beginning of your code.

Glad you figured it out :slight_smile:

what if there are too many elements in the array? i<size might not work…
You can check this if you like…

function chunkArrayInGroups(arr, size) {
  var a = 0;      // for indexStart

 var result=[];
for(i=0;i<arr.length;i+=size){ // for indexEnd
  result.push(arr.slice(a,size+i)); // 
  a+=size; // to change indexStart after each loop

return result;

chunkArrayInGroups(["a", "b", "c", "d","a", "b", "c", "d"], 2);

My approach :slight_smile: had to do some math but iNightElf’s approach seems niter.
` function chunkArrayInGroups(arr, size) {
// Break it up.
var newArr = [];
for (var i = 0; i < arr.length /size; i++) {
var y = newArr.push(arr.slice(sizei,sizei+size));

return newArr;

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

1 Like
+------------**** Solution (Chunky Monkey)****-----------+

function chunkArrayInGroups(arr, size){

var array = []; var splicedArray;

for(var i = 0; i < arr.length; i+size){

splicedArray = arr.splice(i,size);


return array;