Chunky Monkey - help

Chunky Monkey - help
0

#1

Tell us what’s happening:
I’ve got to this stage, I figure I have complicated this a bit cause the for loop in the current state seems unnecessary yet I know the for loop is crucial. I understand that the for loop needs to keep looping after pushing in the size. Help me how.

Your code so far

function chunkArrayInGroups(arr, size) {
    // Break it up.
    var final = [];
    var bla = [];

    for (var i = 0; i < arr.length; i++) {
        bla.push(arr[i]);
    }



    var subArr = bla.slice(0, size);

    final.push(subArr);

    while (final.length < size) {
        final.push(subArr);
    }



    return final;
}

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

Your browser information:

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

Link to the challenge:


#2

I used arr.shift() to iterate through the array instead of using a for loop and bracket notation. Removes the first element of an array and returns it. Give that a try and if you are still stuck I can share my code with you. Good luck!


#3

This part of your code is not neccessary, because you are just recreating the original arr. Knowing this, you can just use arr instead of bla for the rest of your solution.

    var bla = [];

    for (var i = 0; i < arr.length; i++) {
        bla.push(arr[i]);
    }

Now let’s see what the rest of your solution is doing (assuming we use arr instead of bla now).

var subArr = arr.slice(0, size); // creates the first "chunk"

final.push(subArr); // pushes the above chunck into subArr

So far so good. Next we get to your while loop:

while (final.length < size) {
    final.push(subArr);
}

Not sure what you were trying to do with the above code, but what it does is push the same subArr as long as final.length is less than size. Hence, the final version of final will have the same number of elements (subArr) as size.

I suggest writing out an algorithm and before writing any code, you should work through a few of the test cases with your algorithm steps on paper to confirm your algorithm is solid first. If your logic is not correct, you are just going to be guessing what code to use to solve the challenge.