Bird
Raised Fist0
Javascriptprogramming~10 mins

Stack overflow concept in Javascript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that calls itself.

Javascript
function recurse() {
  console.log('Hello');
  [1]();
}
recurse();
Drag options to blanks, or click blank then click option'
Arecurse
Bconsole
Clog
Dcall
Attempts:
3 left
💡 Hint
Common Mistakes
Using console or log instead of the function name.
Trying to call a method that doesn't exist.
2fill in blank
medium

Complete the code to add a base case that stops recursion when n is 0.

Javascript
function countdown(n) {
  if (n [1] 0) {
    return;
  }
  console.log(n);
  countdown(n - 1);
}
countdown(3);
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < instead of ==, which leads to incorrect output.
Using != which does not stop at zero.
3fill in blank
hard

Fix the error in the recursive function to avoid infinite recursion.

Javascript
function sum(n) {
  if (n <= 0) {
    return 0;
  }
  return n + sum([1]);
}
console.log(sum(3));
Drag options to blanks, or click blank then click option'
An
Bn - 1
Cn + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 to n causes infinite recursion.
Passing n unchanged causes infinite recursion.
4fill in blank
hard

Fill both blanks to create a recursive factorial function with a base case.

Javascript
function factorial(n) {
  if (n [1] 1) {
    return 1;
  }
  return n * factorial([2]);
}
console.log(factorial(4));
Drag options to blanks, or click blank then click option'
A<=
B-1
Cn - 1
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == in base case but not handling n=0 properly.
Using -1 instead of n - 1 in recursive call.
5fill in blank
hard

Fill all three blanks to create a recursive function that counts down and stops at zero.

Javascript
function countDown(n) {
  if (n [1] 0) {
    return;
  }
  console.log([2]);
  countDown([3]);
}
countDown(3);
Drag options to blanks, or click blank then click option'
A==
Bn
Cn - 1
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of > in base case returns immediately without printing numbers.
Printing n - 1 instead of n.
Calling countDown with n instead of n - 1.

Practice

(1/5)
1. What causes a stack overflow error in JavaScript?
easy
A. Declaring global variables
B. Using too many variables in a function
C. Too many nested function calls without stopping
D. Calling a function only once

Solution

  1. Step 1: Understand what the call stack is

    The call stack keeps track of function calls in JavaScript. Each call uses some memory.
  2. Step 2: Identify what causes overflow

    If functions call themselves too many times without stopping, the stack runs out of memory, causing overflow.
  3. Final Answer:

    Too many nested function calls without stopping -> Option C
  4. Quick Check:

    Stack overflow = too many nested calls [OK]
Hint: Stack overflow = infinite or too deep recursion [OK]
Common Mistakes:
  • Thinking global variables cause stack overflow
  • Confusing variable count with call stack size
  • Assuming single function call causes overflow
2. Which of the following JavaScript function definitions will cause a stack overflow when called?
easy
A. function infinite() { infinite(); }
B. function countDown(n) { if(n <= 0) return; countDown(n - 1); }
C. function greet() { console.log('Hello'); }
D. function add(a, b) { return a + b; }

Solution

  1. Step 1: Check each function for recursion

    function countDown(n) { if(n <= 0) return; countDown(n - 1); } has a base case stopping recursion. function infinite() { infinite(); } calls itself endlessly without a base case. Options C and D are normal functions without recursion.
  2. Step 2: Identify which causes stack overflow

    Only function infinite() { infinite(); } causes infinite recursion, leading to stack overflow error.
  3. Final Answer:

    function infinite() { infinite(); } -> Option A
  4. Quick Check:

    Infinite recursion = stack overflow [OK]
Hint: Infinite recursion without base case causes overflow [OK]
Common Mistakes:
  • Assuming all recursive functions cause overflow
  • Ignoring base case in recursion
  • Confusing normal functions with recursive ones
3. What will be the output of this code?
function recurse(n) {
  if (n === 0) return 'Done';
  return recurse(n - 1);
}
console.log(recurse(3));
medium
A. 3
B. Stack overflow error
C. undefined
D. "Done"

Solution

  1. Step 1: Trace the recursive calls

    recurse(3) calls recurse(2), then recurse(1), then recurse(0). At n=0, it returns 'Done'.
  2. Step 2: Return values propagate back

    Each call returns the result of the next call, so finally recurse(3) returns 'Done'.
  3. Final Answer:

    "Done" -> Option D
  4. Quick Check:

    Base case stops recursion, returns 'Done' [OK]
Hint: Base case returns value, recursion unwinds safely [OK]
Common Mistakes:
  • Expecting stack overflow without base case
  • Thinking recursion returns input number
  • Ignoring return propagation
4. Find the error in this recursive function that causes a stack overflow:
function factorial(n) {
  return n * factorial(n - 1);
}
medium
A. Wrong multiplication operator
B. Missing base case to stop recursion
C. Function name is incorrect
D. Should use addition instead of multiplication

Solution

  1. Step 1: Check for base case in recursion

    The function calls itself without any condition to stop, so recursion never ends.
  2. Step 2: Understand stack overflow cause

    Without a base case, calls pile up until memory runs out, causing stack overflow.
  3. Final Answer:

    Missing base case to stop recursion -> Option B
  4. Quick Check:

    Base case missing = infinite recursion [OK]
Hint: Always add base case to recursive functions [OK]
Common Mistakes:
  • Assuming multiplication causes error
  • Ignoring need for base case
  • Thinking function name affects recursion
5. You want to safely calculate the sum of numbers from 1 to n using recursion without causing stack overflow. Which change should you make to this function?
function sum(n) {
  return n + sum(n - 1);
}
hard
A. Add a base case: if (n <= 0) return 0;
B. Change + to * operator
C. Call sum(n + 1) instead
D. Remove the recursive call

Solution

  1. Step 1: Identify missing base case

    The function calls itself with n-1 but never stops, causing overflow.
  2. Step 2: Add base case to stop recursion

    Adding if (n <= 0) return 0; stops recursion when n reaches 0 or less.
  3. Step 3: Confirm safe recursion

    With base case, recursion ends properly and sum is calculated without overflow.
  4. Final Answer:

    Add a base case: if (n <= 0) return 0; -> Option A
  5. Quick Check:

    Base case stops recursion safely [OK]
Hint: Base case prevents infinite recursion and overflow [OK]
Common Mistakes:
  • Changing operator instead of adding base case
  • Increasing n causing infinite recursion
  • Removing recursion loses function purpose
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard