Re-create Lodash Library - chunk() method

Hello everyone! I got stuck at this task:
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
        return arrayChunks

I will appreciate any help!

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

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.

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)

1 Like

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);
        return arrayChunks

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.

All right, I will :slight_smile: thx!

1 Like

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)
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.)

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


I was thinking the same until I realized the difference between both of them += is the same as
i = i + size
writing the += saves some time.

What is wrong with my code?

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

From what I can tell it is verbatim to what is in the video, but it doesn’t pass correctly.

I keep getting this:

1 - _.chunk() is defined - Passed!
      let arrayChunk = array.slize(i, i+size);

TypeError: array.slize is not a function
    at Object.chunk (/home/ccuser/workspace/underscore-javascrip
    at Object.<anonymous> (/home/ccuser/workspace/underscore-jav
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

The interpreter is pointing to the misspelling in this line.

1 Like

That was it! Thank you once again sir!

1 Like