Challenge - 5 Problems
Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a typed callback function
What is the output of this TypeScript code when the callback is called?
Typescript
function processNumber(num: number, callback: (n: number) => number): number { return callback(num * 2); } const result = processNumber(5, (x) => x + 3); console.log(result);
Attempts:
2 left
💡 Hint
Remember the callback receives num * 2 and adds 3.
✗ Incorrect
The function doubles 5 to 10, then the callback adds 3, resulting in 13.
❓ Predict Output
intermediate2:00remaining
Return type of a callback with void
What will be logged when this TypeScript code runs?
Typescript
function executeCallback(cb: (msg: string) => void) { cb('Hello'); return 'Done'; } const output = executeCallback((text) => console.log(text.length)); console.log(output);
Attempts:
2 left
💡 Hint
The callback logs the length of the string, then the function returns 'Done'.
✗ Incorrect
The callback logs 5 (length of 'Hello'), then 'Done' is logged after.
🔧 Debug
advanced2:00remaining
Identify the type error in callback usage
Which option shows the correct callback type to fix the TypeScript error in this code?
Typescript
function fetchData(callback: (data: string) => void) { callback('data loaded'); } fetchData((info: number) => console.log(info));
Attempts:
2 left
💡 Hint
The callback expects a string parameter, not a number.
✗ Incorrect
The callback must accept a string because fetchData calls it with a string.
📝 Syntax
advanced2:00remaining
Correct callback type syntax
Which option correctly types a callback that takes two numbers and returns a boolean?
Attempts:
2 left
💡 Hint
Arrow function type syntax uses '=>' without function body.
✗ Incorrect
Option D correctly declares the callback type signature without implementation.
🚀 Application
expert3:00remaining
Using callback types with optional parameters
Given this function, which callback type allows the second parameter to be optional?
Typescript
function operate(x: number, y: number, cb: (a: number, b?: number) => number) { return cb(x, y); } const result = operate(4, 5, (m, n) => n ? m + n : m); console.log(result);
Attempts:
2 left
💡 Hint
Optional parameters use '?' after the parameter name.
✗ Incorrect
Option C correctly marks the second parameter as optional and returns a number.