JS Built-ins
TypeScriptintermediate

Advanced β€” windowed

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

windowed returns all consecutive sub-arrays (windows) of the given size.

Advanced: Sliding Window

Challenge: Implement windowed<T>(arr: T[], size: number): T[][] returning all contiguous subarrays of length size.

windowed([1,2,3,4,5], 3)  // β†’ [[1,2,3],[2,3,4],[3,4,5]]
πŸ’‘ Hints (1)
  • β€’ Iterate while i + size <= arr.length, slice from i to i+size.