The base case is the condition that stops the recursion. It prevents infinite calls by providing a simple answer without further recursion.
Click to reveal answer
beginner
What is the recursive case in recursion?
The recursive case is the part where the function calls itself with a smaller or simpler input to solve the problem step by step.
Click to reveal answer
intermediate
Why do we need both base case and recursive case in a recursive function?
The base case stops the recursion to avoid infinite loops, and the recursive case breaks the problem into smaller parts to solve it gradually.
Click to reveal answer
beginner
Consider this code snippet:<br><pre>function factorial(n) {<br> if (n === 0) return 1;<br> return n * factorial(n - 1);<br>}</pre><br>Identify the base case and recursive case.
Base case: if (n === 0) return 1; Recursive case: return n * factorial(n - 1);
Click to reveal answer
beginner
What happens if a recursive function has no base case?
The function will keep calling itself forever, causing a stack overflow error because it never stops.
Click to reveal answer
What is the purpose of the base case in recursion?
ATo print the result
BTo call the function again
CTo make the problem bigger
DTo stop the recursion
✗ Incorrect
The base case stops the recursion to prevent infinite calls.
In the recursive case, the function usually calls itself with:
AA larger input
BA smaller or simpler input
CThe same input
DNo input
✗ Incorrect
The recursive case calls the function with a smaller or simpler input to solve the problem step by step.
What will happen if a recursive function lacks a base case?
AIt will cause an infinite loop and crash
BIt will run faster
CIt will return immediately
DIt will print the input
✗ Incorrect
Without a base case, recursion never stops, causing infinite calls and a crash.
In this code, what is the base case?
function sum(n) { if (n <= 0) return 0; return n + sum(n - 1); }
Areturn n + sum(n - 1);
Bn - 1
Cif (n <= 0) return 0;
Dfunction sum(n)
✗ Incorrect
The base case is the condition that stops recursion: if (n <= 0) return 0;
Which part of recursion breaks the problem into smaller parts?
ARecursive case
BBase case
CFunction declaration
DReturn statement
✗ Incorrect
The recursive case breaks the problem into smaller parts by calling the function itself.
Explain in your own words what the base case and recursive case are in recursion.
Think about how recursion stops and how it continues.
You got /3 concepts.
Describe what would happen if a recursive function does not have a base case.
Imagine a function calling itself forever without stopping.
You got /3 concepts.
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));