Chunky Monkey challenge. I'm stuck [SOLVED]

Chunky Monkey challenge. I'm stuck [SOLVED]
0

#1

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?


#2

My advice is tell which problem you are stuck on and post any written algorithm, pseudo-code, or any JS code you have so far completed. Make sure to ask specific questions about what you are stuck on or don’t understand about the challenge.


#3

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


#4

Post it here and change the title of your original topic to make it clearer what you need help with. This will help other campers using the search functionality to find your question easier.

When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5

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!


#6

Have you written out the algorithm you plan to use in plain language yet? If you have, post it so we can take a look at what you are thinking about implementing. If not, then write something out and the use code to implement your algorithm. If you are not sure your written algorithm is correct, then post it here so we can give you some pointers.

Before writing a single line of code, you have to know what your algorithm is. Otherwise, you are just randomly “trying” pieces of code in the hopes it will solve something.


#7

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.


#8

Until you can explain your algorithm you are trying to use, it is going make it much more difficult to code a solution. In general, the guide should be a last resort. You should try to figure these challenges out on your own. It will make you a better developer in the long run.

If you don’t think you need to write out the algorithm but still can not figure out what code to type, then I have no further suggestions beyond what I said in my previous reply.

If you decide to write out an algorithm and are not sure if it is correct, I will keep a watch on this thread and respond with any help I can give.


#9

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.


#10

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.


#11

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.


#12
var arr1=arr.slice(0,size);

The above always slices from the very beginning of arr (index 0) and up to index (value of size). So in the call chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3), your code is going to slice index 0 up to index 3 (thru index 2) so it will only push [0,1,2] each time into the end array.

You are currently changing i in your for loop declaration, so take advantage of the fact i has a different value each during each iteration.

Put some console.log statements in your code to see the value of i and size and you should be able to figure out how to use them differently in your code.


#13

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.


#14

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 ], [], [] ].


#15

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?


#16

Don’t worry how long it takes to solve a particular problem. I have sometimes spent hours/days on a problem just trying to develop an algorithm (not even coding). Sometimes, you have to walk away for a bit and then come back again with a clear head. Like most things in life, practice makes perfect. The more problems you learn to solve, the more you will start to recognize similarities in certain types of problems and since you have already solved that type in the past, it gets easier when you see it.

Try working some extra challenges on hackerrank.com or codewars.com. Stick with the easy level ones first and then over time work into the intermediate and advanced.


#17

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.