Chunky Monkey flyin

Chunky Monkey flyin
0

#1

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


#2

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)


#3

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.


#4

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


#5

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!


#6

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


#7

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)


#8

@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?


#9

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


#10

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


#11

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


#12

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.


#13

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.


#14
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…


#15

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;
}

#16

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…


#17

I need to push b onto a again?


#18

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