📖 Scenario: Imagine you have a stack of boxes, and you want to count how many boxes are in the stack by opening one box at a time from the top until there are no more boxes left.This project will help you understand how recursion works by simulating this counting process step-by-step.
🎯 Goal: You will write a recursive function called countBoxes that counts the number of boxes in a stack represented by a number. You will also visualize the call stack by printing messages when the function is called and when it returns.
📋 What You'll Learn
Create a variable called totalBoxes with the value 5 representing the number of boxes in the stack.
Create a variable called callCount initialized to 0 to count how many times the recursive function is called.
Write a recursive function called countBoxes that takes a number n and returns the count of boxes.
Inside countBoxes, print a message when the function is called and when it returns to visualize the call stack.
Call countBoxes with totalBoxes and print the final count.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching files in folders, solving puzzles, and processing nested data.
💼 Career
Understanding recursion helps in software development roles that involve algorithms, data processing, and problem-solving.
Progress0 / 4 steps
1
Create the initial data setup
Create a variable called totalBoxes and set it to 5 representing the number of boxes in the stack.
DSA Javascript
Hint
Use const totalBoxes = 5; to create the variable.
2
Add a counter for recursive calls
Create a variable called callCount and set it to 0 to count how many times the recursive function is called.
DSA Javascript
Hint
Use let callCount = 0; to create the counter variable.
3
Write the recursive function
Write a recursive function called countBoxes that takes a parameter n. Inside the function, increase callCount by 1, print "Calling countBoxes with n = " plus the value of n. If n is 0, return 0. Otherwise, return 1 plus the result of calling countBoxes with n - 1. Before returning, print "Returning from countBoxes with n = " plus the value of n.
DSA Javascript
Hint
Remember to increase callCount and print messages before and after the recursive call.
4
Call the function and print the result
Call the function countBoxes with totalBoxes and store the result in a variable called finalCount. Then print "Total boxes counted: " plus finalCount. Also print "Total recursive calls: " plus callCount.
DSA Javascript
Hint
Call countBoxes(totalBoxes), save the result, and print the messages as described.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. It returns immediately without doing anything
B. It causes an infinite loop leading to a stack overflow error
C. It executes only once and stops
D. It throws a syntax error
Solution
Step 1: Understand the role of base case in recursion
The base case stops the recursion by providing a condition to end calls.
Step 2: Analyze what happens without a base case
Without a base case, the function keeps calling itself endlessly, filling the call stack.
Final Answer:
It causes an infinite loop leading to a stack overflow error -> Option B
Quick Check:
No base case = infinite recursion = stack overflow [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Confusing base case with recursive case
Assuming syntax error occurs without base case
2. Which of the following is the correct syntax to define a recursive function countDown in JavaScript?
easy
A. function countDown(n) { if(n <= 0) return; countDown(n - 1); }
B. function countDown(n) { while(n > 0) countDown(n - 1); }
C. function countDown(n) { for(let i=0; i
D. function countDown(n) { if(n < 0) countDown(n + 1); }
Solution
Step 1: Identify correct base case and recursive call
function countDown(n) { if(n <= 0) return; countDown(n - 1); } has a base case (if n <= 0 return) and calls itself with n-1.
Step 2: Check other options for syntax or logic errors
function countDown(n) { while(n > 0) countDown(n - 1); } uses while loop causing infinite recursion; C uses for loop incorrectly; D lacks proper base case.
Final Answer:
function countDown(n) { if(n <= 0) return; countDown(n - 1); } -> Option A
Quick Check:
Base case + recursive call = correct syntax [OK]
Hint: Look for base case and self-call in function [OK]
Common Mistakes:
Using loops instead of recursion
Missing base case
Wrong recursive call argument
3. What is the output of the following code?
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(3));