Lodash - chunk - comparing my solution to the provided one

I was just working on the lodash practice project.

For the last function chunk() I was basically reading the specifications and then implementing the function based on my own ideas before reading the ideate, implement, test parts of the description.

While my solution passes all the tests, the ideate and implement parts suggest a completely different approach using a for loop with an incrementing counter and if conditions.
Of course I know there are different ways to achieve the same result but the solution explicitly says to not use a while loop.

My solution uses a while loop and I personally prefer it to the one suggested in the exercise because I think it’s shorter and easier to read.
Now ( because ofc I want to learn good practices) I just want to know If there’s any problem with my approach in comparison to the suggested approach or If I can keep it like this:

  chunk(array, size = 1) {
    const result  = [];
    while(array.length > size) {
      result.push(array.splice(0,size));
    }
    result.push(array);
    return result;
  }

The main issue with your solution is that it is destructive. Since splice() changes the array, then it’s also modifying the array that was passed in. That change is why you were able to use array.length in your while condition.

Using slice() would be an alternative since it returns a portion of the array, but doesn’t change the source.

Here is a sample of what I mean side by side, with your code and the solution I came up with:

function myChunk(arr, size = 1) {
  arrOut = [];
  for (let i = 0; i < arr.length; i += size) {
    arrOut.push(arr.slice(i, i+size));
  }
  return arrOut;
}

function yourChunk(array, size = 1) {
  const result  = [];
  while(array.length > size) {
    result.push(array.splice(0,size));
  }
  result.push(array);
  return result;
}

const testArray = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];

console.log('myChunk -', myChunk(testArray, 5));
console.log('myChunk -', myChunk(testArray, 5));
console.log('\n--------------------------------\n');
console.log('yourChunk -', yourChunk(testArray, 5));
console.log('yourChunk -', yourChunk(testArray, 5));

The output would be the following:

myChunk - [ [ 10, 9, 8, 7, 6 ], [ 5, 4, 3, 2, 1 ] ]
myChunk - [ [ 10, 9, 8, 7, 6 ], [ 5, 4, 3, 2, 1 ] ]

--------------------------------

yourChunk - [ [ 10, 9, 8, 7, 6 ], [ 5, 4, 3, 2, 1 ] ]
yourChunk - [ [ 5, 4, 3, 2, 1 ] ]

The test for chunk would have been more complete if it also confirmed the passed in array wasn’t modified.

Oh, yes I see…
Could I solve it just like this?

function yourChunk([...array], size = 1) {

Sure, you could make a copy of the array like that to work with.

Even an array = array.slice() at the start of the function would make a copy of it and wouldn’t destroy the original since only the reference pointer of the parameter is changed to the new array, not the original array that was passed in.

As far as which is ideal, at this size array it wouldn’t matter at all. If there were a lot more items in the array, then making a copy of it first might not be best practice if it can be avoided with a different loop.