Could someone help me understand why my function isn't properly splitting the array into chunks as intended? I've written a function that is supposed to divide an array into subarrays based on the second argument provided. For example, if we have [1, 2, 3, 4] and the number 2 as the second argument, the output should be [[1, 2], [3, 4]]. However, when I run my code, it only returns the first chunk correctly while subsequent chunks are empty arrays. I'm increasing the index by the value of the second argument in each iteration, so I can't pinpoint why it's not working as expected. Any insights or assistance in identifying where the logic error lies would be greatly appreciated.
let arr = [1, 2, 3, 4, 5, 6]
function test(arr, num) {
let idx = 0;
let newArr = []
while (idx < arr.length) {
newArr.push(arr.slice(idx, num))
idx = idx + num
}
return newArr
}
console.log(test(arr, 2))