JS Built-ins
TypeScriptbeginner

Generics β€” nth<T>

πŸ“– Usage Example
function nth<T>(arr: T[], n: number): T | undefined { return arr[n] }
nth([10, 20, 30], 1)   // β†’ 20

nth<T> retrieves the element at index n, returning undefined for out-of-bounds indices.

Generics: nth<T>

Challenge: Implement nth<T>(arr: T[], n: number): T | undefined returning the element at index n.

nth([10, 20, 30], 1)  // β†’ 20
nth([1, 2, 3], 5)     // β†’ undefined
πŸ’‘ Hints (1)
  • β€’ arr[n] is fine β€” returns undefined automatically when out of range.