Chunky Monkey(0)

Chunky Monkey(0)
0

#1

Tell us what’s happening:
I’m making a function that splits an array into chunks of whatever number is put into the size parameter. My code doesn’t seem to be working, though the logic looks to me like it should work. It keeps saying that there is a type error and that it can’t set a property ‘0’ of undefined.

Your code so far

function chunkArrayInGroups(arr, size)
{
  // Break it up.
  var newArr;
  var count = 0;
  var i = 0;
  var grouping = arr.length/(arr.length/size); //For example, an array of 6 in groups of 2 is an array of 6 in groups of 6/(6/2) or 6/(3) = 2
  
  while(i < grouping) 
  {
    for(var j = 0; j < arr.length; j++)
      {
        newArr[i][count] = arr[j];
        count++;
        
        if(count === grouping)
          {
            i++;
            count = 0;
          }
      }
  }
  return newArr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

newArr is left undeclared. Try setting var newArr = []. After that you’ll have the same problem when you try to access newArr[i][count], because newArr[i] is also undefined.

Try setting newArr[i] to an empty array too, but don’t forget to check if it already exists (because otherwise you might overwrite existing data).

for (var j = 0; ...) {
  if (!newArr[i]) {
    newArr[i] = [];
  }
}

You’ll still have failing tests after this though.

Also, it’s a good idea to use Math.floor when dealing with dividing discrete quantities (like array lengths). But when you think about it, the division is redundant, because size already takes the role that grouping does (and the division simplifies to just size anyway).


#3

This is what I have now.


function chunkArrayInGroups(arr, size)
{
  // Break it up.
  var newArr = [[],[]];
  var count = 0;
  var i = 0;
  
  while(i < size) 
  {
    for(var j = 0; j < arr.length; j++)
      {
        newArr[i][count] = arr[j];
        count++;
        
        if(count === size)
          {
            i++;
            count = 0;
          }
      }
  }
  return newArr;
}

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

It worked for chunkArrayInGroups([“a”, “b”, “c”, “d”], 2). I have no idea what’s wrong here.


#4

Tale a look at your most recent solution with a console.log statement showing the variables’ values at the start of each iteration of the for loop. You will see when i = 2 and j = 0, newArr[i] (newArr[2]) is undefined, because at the end of the previous iteration, newArr only had two elements [0,1,2[ and [3,4,5], so when your code tries to reference newArr[2] (a 3rd element) it evaluates to undefined and hence the error “TypeError: Cannot set property ‘0’ of undefined”


#5

Okay, so from this I see that I need to create an array capable of holding more than two other arrays. How do I do this?


#6

An array you create can hold as many other arrays as you want to push into it. Based on your solution, you start with an array with two empty subarrays. It would be better to just start with an empty array newArr (with no subarrays) and then push the subarrays (the chunks of length size) when you need to.

Let’s say you start with:

newArr = [];

and wanted to push a new element (an array with three elements) into newArr, you could do:

var subArr = ['a','b','c']; // an array with 3 elements
var newArr.push(subArr); // adds subArr to newArr
console.log(newArr); // yields [ ['a','b','c'] ]

To solve this challenge, you need to think about building the subarrays first and then pushing them into newArr when the length of the subarray is no more than size. Notice I did not say the subarray’s length has to be equal to size. The reason I say that is you started with an array with 5 elements [0, 1, 2, 3, 4] and size is 2, then newArr will have two subarrays of size 2 and one subarray of size 1.