JS Built-ins
TypeScriptintermediate

Generics β€” chunk<T>

πŸ“– Usage Example
chunk([1, 2, 3, 4, 5], 2)   // β†’ [[1, 2], [3, 4], [5]]

chunk<T> splits an array into sub-arrays of a fixed size, preserving the element type.

Generics: chunk<T>

Challenge: Implement chunk<T>(arr: T[], size: number): T[][] splitting arr into subarrays of size.

chunk([1,2,3,4], 2)    // β†’ [[1,2],[3,4]]
chunk([1,2,3,4,5], 2)  // β†’ [[1,2],[3,4],[5]]
πŸ’‘ Hints (1)
  • β€’ Loop with step = size, use slice to grab each chunk.