Chunky Monkey challenge. I'm stuck [SOLVED]

I’ve decided since the beginning to just advance in order regarding the challenges at FCC, meaning that no matter how long it would take, I’d advance in order. But now I’m stuck for more than a week on one, after solving one per day. I seriously have no idea how to solve it. So my question is- should I continue with the next ones or with the ones that I can solve, and then come back, or should I stay until I get it? I’ve guessed that It would be pointless to move on as the challenges supposes to increase in difficulty. But maybe my strategy is wrong, and I should move to next and leave this one temporary. Care to give me some advice?

Thank you, Randell. Should I post here or create a new thread?

I’ve done nothing, actually. I’ve just the iterated array and I don’t know how to use the slice method.

function chunkArrayInGroups(arr, size) {
  
  var end=[];
  var sliced=arr.slice(0,size);
  
  for(var i = 0; i<arr.length; i++) {     
   
    var item=arr[i];
   
  }end.push(sliced);
  console.log(end);
}

If you could lead me without any snippet so I can get it, the better. Thank you!

Randell, I haven’t. I never did. So far the guide given in the challenge, and the guide on the site were enough to move on.
These are the instructions I have:

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

Hint: 1
The links above suggest to use Array.push(), so let’s start by first creating a new array to store the smaller arrays we will soon have

Hint: 2
Next we’ll need a for loop to loop through arr.

Hint: 3
Finally, we need a method to do the actual splitting and we can use Array.slice() to do that. The key to this Algorithm is understanding how a for loop, size, Array.slice() and Array.push() all work together.

I’ve made tests with slice and splice, with and without the iterator that got me close, but they’re not what the challenge requests.

I didn’t know I needed this until now that you suggested me. I never wrote one, so I’m not sure how to do it. This is my try:

  1. Create an empty array to add the sliced subarrays.
  2. Iterate the input array to get each element.
  3. I must use slice method so that the iteration is made each size’s value times
  4. I push the sliced elements on new arrays.
  5. I should probably let the iteration know where to stop, so each array is of the size of the given value and doesn’t keep iterating, and move on the next items.

If this is wrong, perhaps you could point me to a template of how should be done/written. I’m not sure if I’m missing a step either.

1 Like

That’s a very quite a good attempt, actually. That’s exactly what you need to do.

Now, that you have the steps, convert each step into code. If you encounter, a problem, change the order of the steps, if you have to and do that until all your conditions are met.

One more important thing to ensure is that you consider every use case, for example - what if the 2nd argument is greater than the actual length of the array provided, or what if the array is actually empty. In this way you are making your code error proof.

Keep at it.

2 Likes

I’ve managed to get here:

function chunkArrayInGroups(arr, size) {
  
  var end=[];
    
  for(var i = 0; i<arr.length; i+=size) {
  
    var arr1=arr.slice(0,size);
   
   end.push(arr1); 
  }
  console.log(end);
}

Test case:

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3); //should return [[0, 1, 2], [3, 4, 5], [6]].

So, I have the arrays of the size of size, but I can’t figure out how to tell the iterator to keep moving to next group of items.

Can’t figure it out. Sorry I’ve wasted your time.

I assume due the first answer that is not suggested to move forward if I’m stuck, so that’s pretty much the answer I needed, actually.

You can close this case or whatever it’s needed to do to set it as closed.
Really appreciated your time and help.

I will give you a hint:

You have the correct for loop declaration:

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

Since the first iteration will start with i = 0 and the second iteration will be i = 3 and the third iteration i = 6
and since the first parameter of slice dictates the starting index of the slice and the second parameter of slice dictates the end (up to not including end) index, then you could use:

var arr1=arr.slice(i,size + ???);

Now variable i will always start with the correct index in arr to make the slice. I left your size variable in there, but you need to add something (represented by the ??? above) to size. If you don’t add something to size, the ending parameter will always be 3 (in this example) which will only work in the first iteration. Other iterations would slice an empty array, resulting in newArray looking like [ [ 0, 1, 2 ], [], [] ].

2 Likes

The answer is size+i.
I understand why I must use i as first value for the slice, as it will hold the current array item at the same time as iterates, but probably would never arrive to the conclusion that the ending must be size+i. Although, I understand now that adding the i to the size, makes the slicer to move n positions to continue with the next items.

I feel I’ve done nothing to solve the challenge and this one supposes to be easy. Spent two weeks on it.
Do you have some advice for me on that?

I sincerely thank you for your help and advice. If I’m stuck again (hope not too soon) I wish we could interact again. Congratulations on your work.