Write a function `processBatch(items, batchSize, fn)` that processes items in batches of batchSize, calling fn with each batch.
Problem Statement
Write a function `processBatch(items, batchSize, fn)` that processes items in batches of batchSize, calling fn with each batch.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Slice the array into chunks of batchSize.
š” Hint 2:
Iterate with a step of batchSize using a for loop.
š” Hint 3:
for (let i = 0; i < items.length; i += batchSize) fn(items.slice(i, i + batchSize));
ā
Solution:
```javascript
function processBatch(items, batchSize, fn) {
const results = [];
for (let i = 0; i < items.length; i += batchSize) {
results.push(fn(items.slice(i, i + batchSize)));
}
return results;
}
```