0
0
Typescriptprogramming~20 mins

Generic functions with arrays in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic function that reverses an array
What is the output of this TypeScript code that uses a generic function to reverse an array?
Typescript
function reverseArray<T>(items: T[]): T[] {
  return items.slice().reverse();
}

const numbers = [1, 2, 3];
const reversed = reverseArray(numbers);
console.log(reversed);
ATypeError at runtime
B[1, 2, 3]
C[3, 1, 2]
D[3, 2, 1]
Attempts:
2 left
💡 Hint
Think about what the reverse() method does to an array copy.
🧠 Conceptual
intermediate
2:00remaining
Understanding generic type constraints in functions
Which option correctly describes the purpose of the generic constraint in this function signature? function logLength(item: T): void { console.log(item.length); }
AIt restricts the function to only accept arrays as arguments.
BIt ensures the argument has a length property so the function can safely access item.length.
CIt forces the function to return the length of the argument.
DIt makes the function accept only strings as arguments.
Attempts:
2 left
💡 Hint
Think about what the constraint means for the argument type.
🔧 Debug
advanced
2:00remaining
Identify the error in this generic function using arrays
What error will this TypeScript code produce when compiled? function firstElement(arr: T[]): T { return arr[0]; } const empty: number[] = []; const first = firstElement(empty); console.log(first);
Typescript
function firstElement<T>(arr: T[]): T {
  return arr[0];
}

const empty: number[] = [];
const first = firstElement(empty);
console.log(first);
ANo error, output is undefined
BTypeError: Cannot read property '0' of undefined
CCompilation error: Type 'undefined' is not assignable to type 'T'
DRuntime error: Index out of bounds
Attempts:
2 left
💡 Hint
Consider what happens when accessing the first element of an empty array in JavaScript/TypeScript.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a generic function that returns the last element of an array?
Choose the correct TypeScript syntax for a generic function lastElement that returns the last item of an array of type T.
Afunction lastElement<T>(arr: T[]): T { return arr[arr.length - 1]; }
Bfunction lastElement<T>(arr: T[]): T { return arr.pop(); }
Cfunction lastElement<T>(arr: T[]): T { return arr[-1]; }
Dfunction lastElement<T>(arr: T[]): T { return arr.slice(-1); }
Attempts:
2 left
💡 Hint
Remember how to access the last element of an array by index.
🚀 Application
expert
2:00remaining
What is the output of this generic function that merges two arrays?
Consider this TypeScript generic function that merges two arrays: function mergeArrays(arr1: T[], arr2: U[]): (T | U)[] { return [...arr1, ...arr2]; } const result = mergeArrays([1, 2], ['a', 'b']); console.log(result);
Typescript
function mergeArrays<T, U>(arr1: T[], arr2: U[]): (T | U)[] {
  return [...arr1, ...arr2];
}

const result = mergeArrays([1, 2], ['a', 'b']);
console.log(result);
A[1, 'a', 2, 'b']
B[[1, 2], ['a', 'b']]
C[1, 2, 'a', 'b']
DTypeError at runtime
Attempts:
2 left
💡 Hint
Think about what the spread operator does when merging arrays.