📖 Scenario: Imagine you have a stack of boxes, and you want to count how many boxes are in the stack by taking one box off at a time until none are left.
🎯 Goal: You will write a recursive function in JavaScript that counts down from a number to zero using recursion. This will teach you how to set a base case and a recursive case in a function.
📋 What You'll Learn
Create a function called countDown that takes one parameter n
Use a base case to stop recursion when n is zero
Use a recursive case to call countDown with n - 1
Print the current value of n in each call
Call the function with the number 5
💡 Why This Matters
🌍 Real World
Recursion is used in many places like searching files in folders, solving puzzles, and processing nested data.
💼 Career
Understanding recursion helps in coding interviews and solving problems that involve repetitive tasks or nested structures.
Progress0 / 4 steps
1
Create the function skeleton
Create a function called countDown that takes one parameter n.
DSA Javascript
Hint
Start by writing the function header with the name countDown and one parameter n.
2
Add the base case
Inside the countDown function, add a base case that stops the recursion when n is equal to 0. Use an if statement to check if n === 0 and return from the function.
DSA Javascript
Hint
The base case stops the function from calling itself when n reaches zero.
3
Add the recursive case
Inside the countDown function, after the base case, add a console.log(n) statement to print the current value of n. Then call countDown recursively with n - 1.
DSA Javascript
Hint
The recursive case prints n and then calls countDown with one less than n.
4
Call the function and see the output
Call the countDown function with the argument 5 and print the output.
DSA Javascript
Hint
Call countDown(5) to start counting down from 5 to 1.
Practice
(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To call the function again with a smaller input
B. To increase the input size for the next call
C. To stop the recursion and prevent infinite calls
D. To print the result of the recursion
Solution
Step 1: Understand what recursion does
Recursion calls the same function repeatedly with smaller inputs.
Step 2: Identify the role of the base case
The base case stops the recursion by providing a condition where no further calls happen.
Final Answer:
To stop the recursion and prevent infinite calls -> Option C
Quick Check:
Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls recursion again
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in this recursive function to calculate factorial?
function factorial(n) {
if (___) {
return 1;
}
return n * factorial(n - 1);
}
easy
A. n > 0
B. n === 0
C. n === 1
D. n < 0
Solution
Step 1: Recall factorial base case
Factorial of 0 is 1, so base case is when n equals 0.
Step 2: Match condition to code
The condition n === 0 correctly stops recursion and returns 1.
Final Answer:
n === 0 -> Option B
Quick Check:
Base case for factorial = n === 0 [OK]
Hint: Factorial base case is n equals zero [OK]
Common Mistakes:
Using n > 0 as base case causes infinite recursion
Using n < 0 which is invalid for factorial
Using n === 1 misses factorial(0) base case
3. What will be the output of this recursive function call?
function sumTo(n) {
if (n === 0) {
return 0;
}
return n + sumTo(n - 1);
}
console.log(sumTo(3));