Re-create Lodash Library - chunk() method

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!

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

1 Like

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);
arrayChunks.push(arrayChunk);
}
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 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)
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.)

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

2 Likes

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);
arrayChunks.push(arrayChunk);
}
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!
/home/ccuser/workspace/underscore-javascript-capstone/_.js:87
let arrayChunk = array.slize(i, i+size);
^

TypeError: array.slize is not a function
at Object.chunk (/home/ccuser/workspace/underscore-javascrip
t-capstone/_.js:87:30)
at Object.<anonymous> (/home/ccuser/workspace/underscore-jav
ascript-capstone/test/chunk.js:15:107)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
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

Hello there people! Not sure what’s wrong Checked multiple times. the lat method gets the same error. Would love some help.

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;
}```

I keep getting this message:

\$ node test/chunk.js
/home/ccuser/workspace/underscore-javascript-capstone/_.js:75
for (let i = 0, i < array.length, i += size) {
^
SyntaxError: Identifier ‘i’ has already been declared
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/chunk.js:1:73)```

That’s a weird one. What happens if you change the name of the variable?

I tried to change the variable into ( let j =0, …) . Same thing. The console says such variable has already been declared

Is this some sort of a bug from the Codecademy’s side? Since I researched all internet and couldn’t find any references

We could go down the list of letters, or try,

for (let pointer = 0; ...

What is the outcome?

Nope(( Same outcome. Error

Can we see you complete _ object, please?