Recreating lodash library - chunk method

(https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash)
In the lodash library project (where we recreate methods from lodash), we are asked to write a method for chunk such that:
- It takes two arguments: an array and a size
- it breaks up the supplied array into arrays of the specified size
- it returns an array containing all of the previously-created array chunks in the
order of the original array
- If the array can’t be broken up evenly, the last chunk will be smaller than the
specified size
- If no size is supplied to the method, the size is set to 1

I entered the code below, but it’s not working and I don’t know why.

chunk(array, size) {
if (size===undefined) {
size = 1
} let newArray = ;
while (array.length >= size) {
newArray.push(array.splice(0, size));
} if array.length < size {
newArray.push(array);
} return newArray;
}

Your attacking the problem from the wrong direction.

What you are doing now is the following:

  • check if size is defined.
  • while the length of the complete array is higher or equal to the given size.
    • store array[0] in a new array.
    • cut out array[0] from original array

So what it will do is cut out all the objects in the original array till the original array has a length less than the size.
The newArray will contain all those items you have cut out.
Kinda sounds like the opposite of what you are trying to accomplish.

this is how i would do this.

  • check if size is given.
  • create an array that will contain the results.
  • create an array that will hold the chuck data for us while we are filling it.
  • create a counter that cannot exceed or be equal to size and starts at zero.
  • for every element in given array do the following
    • push current element to array that temporary holds the chunk data.
    • counter++
    • check if counter exceeds or equals size.
      • if yes, add the current chunk to the results.
      • empty chunkHolder for re-use
      • set the counter back to zero for re-use.

I don’t have pro so i am not sure what kind of data comes in the function. but something like this should come closer to the functionality you are looking for.

chunk(array, size) {
	if (size===undefined) 
		size = 1;
	
	let result = [];
	let chunkHolder = [];
	let counter = 0;		// may not exceed or equal size.
	
	for(let i = 0; i < array.length; i++){
		chunkHolder.push(array[i]);
		counter++;
		if(counter >= size){
			result.push(chunkHolder);
			chunkHolder = []; // reset chunkHolder so it can be re-used
			counter = 0; // reset counter so it can be re-used.
		}
	}
	return result;
}

// expected output should be something like this: [[data,data,data], [data,data,data], [data,data,data]]

Code is untested so you might want to tweak it. This is the general idea tho. Also i did not implement the last chunk if the array can’t be broken up evenly. so you wanna check that part aswell.