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