Challenge - 5 Problems
Parameters Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with typed parameters
What is the output of this TypeScript code when calling
greet('Alice', 3)?Typescript
function greet(name: string, times: number): string { return `${name} `.repeat(times).trim(); } console.log(greet('Alice', 3));
Attempts:
2 left
💡 Hint
Look at how the
repeat method works on strings.✗ Incorrect
The function repeats the string "Alice " three times, resulting in "Alice Alice Alice ". The
trim() removes the trailing space, so the output is "Alice Alice Alice".❓ Predict Output
intermediate2:00remaining
Type error with wrong parameter type
What error will TypeScript show for this code?
Typescript
function multiply(a: number, b: number): number { return a * b; } multiply(5, '2');
Attempts:
2 left
💡 Hint
Check the types of arguments passed to the function.
✗ Incorrect
The function expects both parameters to be numbers. Passing a string causes a compile-time type error in TypeScript.
🔧 Debug
advanced2:00remaining
Why does this function cause a type error?
Identify the cause of the type error in this code snippet.
Typescript
function formatDate(date: Date, format: string = 'YYYY-MM-DD'): string { return date.toLocaleDateString(format); } formatDate(new Date());
Attempts:
2 left
💡 Hint
Check the official signature of
toLocaleDateString.✗ Incorrect
The
toLocaleDateString method accepts optional locales and options objects, not a string format. Passing a string causes a type error.📝 Syntax
advanced2:00remaining
Correct parameter type for callback function
Which option correctly types the parameter of this callback function?
Typescript
function processItems(items: string[], callback: (item: string) => void) { items.forEach(callback); } processItems(['a', 'b'], (item) => console.log(item));
Attempts:
2 left
💡 Hint
The array contains strings, so the callback parameter should match that type.
✗ Incorrect
The callback receives each string item and returns nothing (void). Option B matches this signature exactly.
🚀 Application
expert3:00remaining
Determine the type of parameters in a generic function
Given this generic function, what is the type of parameter
value when calling identity(42)?Typescript
function identity<T>(value: T): T { return value; } const result = identity(42);
Attempts:
2 left
💡 Hint
TypeScript infers the type from the argument passed.
✗ Incorrect
When calling
identity(42), TypeScript infers T as number, so value is of type number.