Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a callback function that logs a message.
Node.js
function greet(name, [1]) { console.log('Hello ' + name); [1](); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'promise' or 'async' as a parameter name instead of 'callback'.
Not calling the callback function inside the main function.
✗ Incorrect
The callback function is passed as an argument and called inside the main function.
2fill in blank
mediumComplete the code to call a function with a callback that logs 'Done'.
Node.js
function doTask(cb) {
setTimeout(() => {
console.log('Task complete');
[1]();
}, 1000);
}
doTask(() => console.log('Done')); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a function name not defined as a parameter.
Forgetting to call the callback function.
✗ Incorrect
The callback function passed as 'cb' is called after the task finishes.
3fill in blank
hardFix the error in the nested callbacks to avoid callback hell.
Node.js
function step1(cb) {
setTimeout(() => {
console.log('Step 1');
cb();
}, 500);
}
function step2(cb) {
setTimeout(() => {
console.log('Step 2');
cb();
}, 500);
}
step1(() => {
[1](() => {
console.log('Step 3');
});
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'step1' again inside its own callback causing infinite loop.
Using undefined function names.
✗ Incorrect
To avoid callback hell, call the next step function properly inside the previous callback.
4fill in blank
hardFill both blanks to create a nested callback structure that logs steps in order.
Node.js
function first(cb) {
setTimeout(() => {
console.log('First');
[1]();
}, 300);
}
function second(cb) {
setTimeout(() => {
console.log('Second');
[2]();
}, 300);
}
first(() => {
second(() => {
console.log('Done');
});
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for callback parameters inconsistently.
Not calling the callback functions inside setTimeout.
✗ Incorrect
Both functions call their callback parameter to continue the chain.
5fill in blank
hardFill all three blanks to create a callback chain that logs three steps in order.
Node.js
function one(cb) {
setTimeout(() => {
console.log('One');
[1]();
}, 200);
}
function two(cb) {
setTimeout(() => {
console.log('Two');
[2]();
}, 200);
}
function three(cb) {
setTimeout(() => {
console.log('Three');
[3]();
}, 200);
}
one(() => {
two(() => {
three(() => {
console.log('All done');
});
});
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing callback parameter names inconsistently.
Forgetting to call the callback in any function.
✗ Incorrect
Each function calls its callback parameter to continue the chain.