JS Built-ins
TypeScriptintermediate

Generics β€” zip<T, U>

πŸ“– Usage Example
zip([1, 2, 3], ['a', 'b', 'c'])   // β†’ [[1,'a'], [2,'b'], [3,'c']]

zip<T, U> pairs elements from two arrays into tuples, stopping at the shorter array.

Generics: zip<T, U>

Challenge: Implement zip<T, U>(a: T[], b: U[]): [T, U][] that pairs elements by index, stopping at the shorter array.

zip([1,2,3], ['a','b','c'])  // β†’ [[1,'a'],[2,'b'],[3,'c']]
zip([1,2], ['a','b','c'])    // β†’ [[1,'a'],[2,'b']]
πŸ’‘ Hints (1)
  • β€’ Math.min(a.length, b.length) gives the safe iteration count.