Re-create Lodash Library - chunk() method


#1

Hello everyone! I got stuck at this task: https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash
Especially task 41 point 7 which is:

Within the for loop, create a variable called arrayChunk and set it equal to the chunk of the array going from the current loop index to the current loop index plus size . You an use .slice() to accomplish this.

I don’t understand this point and have no clue how to code it.
This is my code:

chunk (array, size = 1){
        let arrayChunks = [];
        for (let i = 0; i < array.length; i+size ){ // not sure if that's the correct for loop
        let arrayChunk = array.slice();  // no idea what to put there
        arrayChunks.push(arrayChunk);  
      }
        return arrayChunks
      },

I will appreciate any help!


#2

If you have a bunch of… stuff, let’s say numbers
…which are supposed to be split into groups of 3

[1, 1, 1, 2, 1, 1, 1, 1, 1, 3]

Then you’re supposed to create arrays at these places:

[1, 1, 1, 2, 1, 1, 1, 1, 1, 3]
[       ][       ][       ]

(except filled with the numbers from those locations)
(and perhaps one for the 3 at the end as well)

As you iterate over the big array, you can add its elements into the current chunk, either one at a time or by letting slice do some of the heavy lifting

Each time you finish a chunk, put it in the results and start over with an empty chunk


#3

Yeah, I understand what the method should do, but have no idea how to do this. But I think I can see the light in the tunnel.


#4

Maybe the what and the how are the same thing. That is also how to do it.

You could try using pen and paper to make yourself go through all the required actions (which are probably incredibly obvious when you do it, but perhaps only because this time you really work through it)


#5

Alll right! I’ve finally found the solution.

chunk (array, size = 1){
        let arrayChunks = [];
        for (let i = 0; i < array.length; i+=size ){
        let arrayChunk = array.slice(i, i+size);
        arrayChunks.push(arrayChunk);  
      }
        return arrayChunks
      },

#6

This can also be done using a while loop and the drop method we wrote earlier. See what you can come up if you have some time for more practice once you finish the project.


#7

All right, I will :slight_smile: thx!


#8

Hi All,

Here is a great quiz question: why does this code result in a memory crash? It behaves like an infinite loop and crashes any browser that comes near it.

chunk(array, size) {
if(size === undefined) {
size = 1;
}
let arrayChunks = ;
for (i = 0; i < array.length; i + size) {
let arrayChunk = array.slice(i, i + size)
arrayChunks.push(arrayChunk)
}
return arrayChunks
}

Do you guys have an idea why? I’m sure it’s in the line where the for loop parameters are specified.

Many thanks and soldier on everyone!

(update: I think it’s the ‘i + size’ line. still investigating.)


#9

how about trying ‘i+= size’ in the for loop?