Algorithms and Data Structures · Junior · Question 27 of 75

What happens when you call this function?

function countdown(n) {
  console.log(n);
  countdown(n - 1);
}
countdown(3);

Explanation

There is no base case such as if (n === 0) return, so the function goes into negative numbers and keeps calling itself. Each call takes a stack frame, and eventually the stack overflows.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 27

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz