Challenge - 5 Problems
Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding callback execution order
What is the output of the following code?
Javascript
function first(cb) { console.log('First'); cb(); } function second() { console.log('Second'); } first(second); console.log('Third');
Attempts:
2 left
💡 Hint
Remember that callbacks run inside the function when called.
✗ Incorrect
The function 'first' logs 'First', then calls the callback 'second' which logs 'Second'. After 'first' finishes, 'Third' is logged.
❓ Predict Output
intermediate2:00remaining
Callback with asynchronous behavior
What will be printed to the console when this code runs?
Javascript
function asyncOperation(cb) { setTimeout(() => { cb('Done'); }, 100); } asyncOperation((message) => { console.log(message); }); console.log('Start');
Attempts:
2 left
💡 Hint
setTimeout delays the callback, so the last console.log runs first.
✗ Incorrect
The 'Start' message logs immediately. The callback inside setTimeout runs after 100ms, logging 'Done'.
❓ Predict Output
advanced2:00remaining
Callback with error handling
What is the output of this code?
Javascript
function fetchData(callback) { const error = null; const data = { id: 1, name: 'Test' }; callback(error, data); } fetchData((err, result) => { if (err) { console.log('Error:', err); } else { console.log('Data:', result.name); } });
Attempts:
2 left
💡 Hint
Check how the callback handles the error and data parameters.
✗ Incorrect
Since error is null, the else branch runs and logs the data's name property.
❓ Predict Output
advanced2:00remaining
Callback with multiple asynchronous calls
What will be the output of this code?
Javascript
function task1(cb) { setTimeout(() => { console.log('Task 1 done'); cb(); }, 200); } function task2() { console.log('Task 2 done'); } task1(task2); console.log('Start tasks');
Attempts:
2 left
💡 Hint
Remember setTimeout delays the callback, so synchronous logs run first.
✗ Incorrect
The 'Start tasks' logs immediately. After 200ms, 'Task 1 done' logs, then callback runs logging 'Task 2 done'.
❓ Predict Output
expert2:00remaining
Nested callbacks and variable scope
What is the output of this code?
Javascript
function outer(cb) { let count = 0; function inner() { count++; cb(count); } inner(); inner(); } outer((num) => { console.log('Count:', num); });
Attempts:
2 left
💡 Hint
Check how many times the callback is called and what it logs each time.
✗ Incorrect
outer calls inner() twice. Each time, count is incremented (to 1 then 2) and the callback is invoked with the current count, logging 'Count: 1' then 'Count: 2'.