Chunky Monkey(Stuck-ish)

Tell us what’s happening:

The below code works for only one case and I understand that now. I also know that a for loop is inevitable to solve for all cases.
My question is why would you want to use a for loop?

I have a feeling that arithmetic would be a better, simpler focus (I would just have to figure out a way to organize my variables so that the arithmetic was focused and more streamlined.)

However, I have also only been doing JS for four months so my feelings are questionable to say the least.

Help please.

Your code so far


function chunkArrayInGroups(arr, size) {
 var newArr = [];
 let num = arr.length/size;
 var arrSize = arr.slice(0, num);
 var arrSizeTwo = arr.slice(num, arr.length);
 newArr.push(arrSize, arrSizeTwo);
  return newArr;
}

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

Your browser information:

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

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

I think you answered your own question. A loop of some sort is needed to accommodate different arr length and size combinations. Suppose you have an array with 50 elements, and chunk size of 2. Then you’d need 25 slice and push statements in your function without a loop.

So far you are breaking an array into two subarrays and sometimes the first subarray is size elements long. That is not what is required in this challenge

Also, let num = arr.length/size; // not always an integer

Can you state an algorithm plainly without using any Javascript? Maybe if you tried to work out the problem in pseudo code first a method would become apparent even if you needed help finding the Javascript to turn that into code.

This isn’t an arithmetic problem, so you will be out of luck trying to solve it with arithmetic. The way you are trying to solve it is also unfeasible, as @ArtemPetrov says. You have a collection of values, you need to iterate over those values. You don’t have to specifically use a for loop to do that, but you need to iterate over them values somehow (unless you hardcode every possible combination, which defeats the point somewhat)

The more I look at this the more the test case example provided don’t seem to make sense.

For example, the first check mark relates to ([“a”, “b”, “c”, "d’], 2). The indicted solution is [[“a”, “b”], [“c”, "d’]], which absolutely makes sense. Within the function parameter 2 indicates the amount of subarrays that should be returned.

However, I become confused by solutions such as the following:

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

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

There’s some more examples like this, but I’m only going to post a few that are representative of my issue so as not to be annoying.

I think that Solution 2. should include 4 sub-arrays. Or, even if I’m supposed to count the main array(and don’t), the solution still doesn’t make sense to me.

Solution 1. utterly confuses me because the size is 2; yet, three sub-arrays are returned. I think I can solve this thing and it seems like a lot of fun but I’m hung up on the logic above. Can someone explain this to me?

Reread the problem carefully. The parameter size is the length of the subarrays, not the number of sub arrays

When I console.log my result, it returns the correct solution according to the test solutions.

However, I don’t pass any of the tests. I have input different content for arr and size ; yet, nothing passes. Where am I messing up?


function chunkArrayInGroups(arr, size) {
  //create empty array
  var chunkArr = [];
  var result;
  //for loop to iterate arr
  for(let i = 0;i < arr.length; i++){
//store .slice() of arr into arrays of size-length 
   var tempOne = arr.slice(size*i, size*(i+1));
    console.log(tempOne);
//.push() different sub-arrays into empty array
    result = chunkArr.push(tempOne);
  }
  return result;
  console.log(result);
}

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

The first three hints are all related to the same line of code below.

This line above is not doing what you think it is. You might want to look up what is returned from array.push() method.

You are pushing your “chunks” onto chunkArr but then returning result. Why?

Try logging your result variable right at the end and you will see that your return does not match what is expected in the challenge tests. Try logging chunkArr also.

Your for-loop is overly complicated and so your call to arr.slice is overly complicated also.

Try replacing this line console.log(tempOne); with this one console.log(i, tempOne); You will see that this line executes more than expected.

Instead of incrementing your for-loop index by 1, try incrementing i by size. That way i will always be the index where the next chunk should start.