Array.prototype.map Polyfill
Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.
JavaScript · Arrays · Polyfills — Pratik Rai ·
Split an array into groups of a fixed size, with whatever is left over in a shorter final group.
Write chunk(arr, size) splitting the array into subarrays of length size.
The final group holds the remainder and may be shorter. A size of zero or less cannot produce groups — return an empty array.
Input:
JSfile.javascript1chunk([1, 2, 3, 4, 5], 2);
Output:
[[1, 2], [3, 4], [5]]
Two full groups and a remainder of one.
slice past the end of an array simply stops, so the short final group needs no special case.Goal: Return groups of the requested size, with a short final group when needed.
Continue learning with these related challenges
Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.
JavaScript · Arrays · Polyfills — Pratik Rai ·
Implement Array.prototype.filter from scratch. The key detail: push the original element, not the boolean result of the predicate.
JavaScript · Arrays · Polyfills — Pratik Rai ·
Implement Array.prototype.flat from scratch with a configurable depth. Default depth is 1, not Infinity.
JavaScript · Arrays · Recursion — Pratik Rai ·
Implement Array.prototype.map from scratch. Master callback signatures, thisArg binding, and sparse-array hole preservation.
JavaScript · Arrays · Polyfills
Pratik Rai ·
Implement Array.prototype.filter from scratch. The key detail: push the original element, not the boolean result of the predicate.
JavaScript · Arrays · Polyfills
Pratik Rai ·
Implement Array.prototype.flat from scratch with a configurable depth. Default depth is 1, not Infinity.
JavaScript · Arrays · Recursion
Pratik Rai ·