Create chunk that splits array into groups of size length. Return empty array if input invalid.
Skip to solutionKEEP THE
mediumDSA
Implement _.chunk(array, size)
650 views
01
Understand the problem
chunkarray
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Approach: Slice in steps of size.
function chunk(array, size = 1) {
if (!Array.isArray(array) || size < 1) return [];
const res = [];
for (let i = 0; i < array.length; i += size) res.push(array.slice(i, i + size));
return res;
}
// Demo
console.log(chunk([1,2,3,4,5], 2)); // [[1,2],[3,4],[
Solution ready — 2 min read
Classified // press E to declassify
04
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 133 of 190 decoded in the JavaScript Coding track. One more won't hurt.