Challenge - 5 Problems
Generic Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this TypeScript code using generic array syntax?
Consider the following TypeScript code snippet. What will be logged to the console?
Typescript
const numbers: Array<number> = [1, 2, 3]; const doubled = numbers.map(x => x * 2); console.log(doubled);
Attempts:
2 left
💡 Hint
Think about what the map function does to each element in the array.
✗ Incorrect
The map function applies the given function to each element of the array, doubling each number. So the output is [2, 4, 6].
❓ Predict Output
intermediate1:30remaining
What is the type of this generic array in TypeScript?
Given this declaration, what is the type of the variable 'words'?
Typescript
let words: Array<string> = ['hello', 'world'];
Attempts:
2 left
💡 Hint
Look at the generic type inside the angle brackets.
✗ Incorrect
The generic type Array means an array where every element is a string.
🔧 Debug
advanced2:00remaining
Why does this TypeScript code cause a compilation error?
Examine the code below. Why does it cause a TypeScript compilation error?
Typescript
const mixed: Array<number> = [1, 2, '3'];
Attempts:
2 left
💡 Hint
Check the types of all elements inside the array.
✗ Incorrect
The array is declared to hold only numbers, but '3' is a string, causing a type mismatch error.
❓ Predict Output
advanced2:00remaining
What is the output of this TypeScript code using generic array syntax with union types?
What will be the output of this code snippet?
Typescript
const items: Array<number | string> = [1, 'two', 3]; const filtered = items.filter(item => typeof item === 'number'); console.log(filtered);
Attempts:
2 left
💡 Hint
The filter keeps only numbers from the array.
✗ Incorrect
The filter function returns only elements whose type is 'number', so the output is [1, 3].
🧠 Conceptual
expert2:30remaining
How many elements are in the resulting array after this TypeScript code runs?
Consider this code snippet. How many elements does the 'result' array contain after execution?
Typescript
function createArray<T>(length: number, value: T): Array<T> { return new Array(length).fill(value); } const result = createArray<string>(3, 'hi');
Attempts:
2 left
💡 Hint
The fill method fills the array with the given value for the specified length.
✗ Incorrect
The function creates an array of length 3, filled with the string 'hi', so the result has 3 elements.