Challenge - 5 Problems
Function Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of nested function calls
What is the output of the following JavaScript code?
Javascript
function first() { return second(); } function second() { return third(); } function third() { return 'Done'; } console.log(first());
Attempts:
2 left
π‘ Hint
Think about how functions return values and how they call each other.
β Incorrect
The function first calls second, which calls third, which returns the string 'Done'. This value is returned back through the call stack and logged.
β Predict Output
intermediate2:00remaining
Understanding asynchronous function execution
What will be printed to the console when this code runs?
Javascript
console.log('Start'); setTimeout(() => console.log('Middle'), 0); console.log('End');
Attempts:
2 left
π‘ Hint
Remember how setTimeout works with the event loop.
β Incorrect
The synchronous console.logs run first, printing "Start" then "End". The setTimeout callback runs after the current call stack is empty, so "Middle" prints last.
π§ Debug
advanced2:00remaining
Identify the error in recursive function
What does countdown(3) return when executed?
Javascript
function countdown(n) { if (n === 0) { return 'Done'; } console.log(n); countdown(n - 1); } countdown(3);
Attempts:
2 left
π‘ Hint
Look at what the function returns and what is logged.
β Incorrect
The function logs numbers 3, 2, 1 but does not return anything from recursive calls, so the overall call returns undefined.
β Predict Output
advanced2:00remaining
Output of function with variable shadowing
What is the output of this code?
Javascript
let x = 10; function test() { let x = 20; function inner() { x++; console.log(x); } inner(); console.log(x); } test(); console.log(x);
Attempts:
2 left
π‘ Hint
Consider which x each function is using and how increments affect them.
β Incorrect
The inner function increments the x declared in test (20 to 21) and logs 21. Then test logs x (21). The global x remains 10.
β Predict Output
expert3:00remaining
Output of complex function execution with closures and async
What will be the output of this code?
Javascript
function createCounter() { let count = 0; return { increment() { count++; console.log(count); }, incrementAsync() { setTimeout(() => { count++; console.log(count); }, 0); } }; } const counter = createCounter(); counter.increment(); counter.incrementAsync(); counter.increment();
Attempts:
2 left
π‘ Hint
Think about when the async increment runs compared to synchronous increments.
β Incorrect
First increment logs 1. Then incrementAsync schedules an increment after current code. Then increment logs 2. After current code finishes, incrementAsync runs, increments count to 3 and logs 3.