0
0
Javascriptprogramming~20 mins

Stack overflow concept in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Overflow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this recursive function?

Consider this JavaScript function that calls itself recursively without a proper stop condition. What will happen when we run countdown(5)?

Javascript
function countdown(n) {
  console.log(n);
  countdown(n - 1);
}
countdown(5);
APrints 5 once and stops
BPrints numbers 5 to 1 then stops
CPrints numbers 5 to 0 then stops
DThrows a RangeError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint

Think about what happens when the function never stops calling itself.

🧠 Conceptual
intermediate
1:30remaining
What causes a stack overflow in JavaScript?

Which of the following best describes what causes a stack overflow error in JavaScript?

ACalling functions recursively without a base case
BUsing asynchronous functions incorrectly
CUsing too many global variables
DDeclaring too many variables inside a function
Attempts:
2 left
💡 Hint

Think about what happens when functions keep calling themselves endlessly.

Predict Output
advanced
2:00remaining
What is the output of this recursive factorial function?

Look at this factorial function. What will factorial(5) return?

Javascript
function factorial(n) {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}
console.log(factorial(5));
AThrows RangeError: Maximum call stack size exceeded
B24
C120
DUndefined
Attempts:
2 left
💡 Hint

Factorial of 5 is 5 * 4 * 3 * 2 * 1.

Predict Output
advanced
1:30remaining
What error does this code produce?

This function calls itself without a base case. What error will appear when running infinite()?

Javascript
function infinite() {
  return infinite();
}
infinite();
ARangeError: Maximum call stack size exceeded
BTypeError: infinite is not a function
CSyntaxError: Unexpected token
DNo error, runs forever
Attempts:
2 left
💡 Hint

Think about what happens when a function calls itself endlessly without stopping.

🧠 Conceptual
expert
2:00remaining
How can you prevent stack overflow in recursive functions?

Which of these is the best way to avoid stack overflow errors in recursive JavaScript functions?

AAvoid using recursion and only use loops
BUse a base case to stop recursion and avoid infinite calls
CUse global variables to store intermediate results
DDeclare all variables outside the function
Attempts:
2 left
💡 Hint

Think about what stops a recursive function from calling itself forever.