Chunky Monkey flyin

Tell us what’s happening:
I’m not sure what I’m missing here. Logically this seems like it should work so maybe I’m missing something simple. Any direction is greatly appreciated.

Your code so far


function chunkArrayInGroups(arr, size) {
  let a = [];
  let b = [];
  let c = 0;
  for (let i=0; i<arr.length;i++) {
    if (c<size) {
      b.push(arr[i]);
      c++;
    } else {
      c=0;
      a.push(b);
      let b = [];
    }
  }
  return a;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey

The problem is what happens when c equals size. You are totally ignoring the value at that index point… (that is you you need to deal with arr[i] at this point but your code ignores it)

That’s a good catch! I added the b.push… to the else statement before the a.push. I’m still getting a b is undefined error.

It’s because of your use of let in that block

Basically what happens is that you’re redefining b in that scope, so it doesn’t have access to the previous b you’ve been using

It’s not defined because the two bs are different variables

If you remove the second let it should be defined at that point as what you were expecting it to be

I removed the 2nd let and that addressed the undefined issue. I’m still missing something as it’s not satisfying any of the checks. Is there a good way to output the array to the console to see what it is returning?

Thanks again for your help!

console.log(arr) should work? A good place to put that would be in your else clause

It’ll output it to your browser’s console rather than the FCC one, which you can access on most browsers with f12

That doesn’t sound logical. Shouldn’t you handle the current arr[i] after resetting b?
To log, use console.log(arr) or whatever you want as param and then view it in the browser’s console (Ctrl- shift- j in chrome and click on console)

@hbar1st You are correct. I don’t want it added before pushing the b array to a but after.

Also, when I use console.log(arr) I just get “unavailable” output to the console. I’m using Firefox at the moment but will be on Chrome later. Maybe that shows differently?

… is c zero after you add arr[i]to b after resetting? Or is it 1?

It should be 0 until it’s incremented in the next loop isn’t it?

But isn’t c the count of things inside b?

It is the count of things inside b[] but it is only identifying when the size number of items have been pushed to b[] from the given array. The else statement only resets and pushes the final b[] to a[] when c items have been pushed to b[]. I’m not sure what I’m still missing here.

I’m trying to give you hints…
can you share your latest code in between two lines with three backticks like this:
```
put code here
```

and after this I will attempt to help further.

function chunkArrayInGroups(arr, size) {
  let a = [];
  let b = [];
  let c = 0;
  for (let i=0; i<arr.length;i++) {
    if (c<size) {
      b.push(arr[i]);
      c++;
    } else {
      c=0; //should c be 0? or 1?
      a.push(b);
      b = [];
      b.push(arr[i])
      console.log(b);
    }
  }
  //something is missing here
  return a;
}

I added some comments with the previous hint and a new hint…

I understand you are helping and I appreciate it! You mean that since I am pushing the current item to b in the else I don’t need c reset to 0 but rather 1?

function chunkArrayInGroups(arr, size) {
  let a = [];
  let b = [];
  let c = 0;
  for (let i = 0; i < arr.length; i++) {
    if (c < size) {
      b.push(arr[i]);
      c++;
    } else {
      c = 1;
      a.push(b);
      b = [];
      b.push(arr[i]);
    }
  }
  return a;
}

yes correct. One more thing is missing…
after you push the last item into b in the top if statement and the loop ends… you need to do something…

I need to push b onto a again?

@hbar1st You are awesome!!! That worked. Thank you for your patience with me on this.

1 Like