0
0
Javascriptprogramming~20 mins

Function execution flow in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Function Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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());
AError: third is not defined
Bundefined
C"Done"
D"first"
Attempts:
2 left
πŸ’‘ Hint
Think about how functions return values and how they call each other.
❓ Predict Output
intermediate
2: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');
A"Start" "End" "Middle"
B"End" "Start" "Middle"
C"Middle" "Start" "End"
D"Start" "Middle" "End"
Attempts:
2 left
πŸ’‘ Hint
Remember how setTimeout works with the event loop.
πŸ”§ Debug
advanced
2: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);
Aundefined
BRangeError: Maximum call stack size exceeded
C"Done"
D3 2 1
Attempts:
2 left
πŸ’‘ Hint
Look at what the function returns and what is logged.
❓ Predict Output
advanced
2: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);
A21 20 10
B20 21 10
C21 20 20
D21 21 10
Attempts:
2 left
πŸ’‘ Hint
Consider which x each function is using and how increments affect them.
❓ Predict Output
expert
3: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();
A1 3 2
B1 2 3
C1 2 2
D1 3 3
Attempts:
2 left
πŸ’‘ Hint
Think about when the async increment runs compared to synchronous increments.