Discover how a function calling itself can solve big problems step by step!
Why Factorial Using Recursion in DSA Javascript?
Imagine you want to find the factorial of a number, like 5! (which means 5 x 4 x 3 x 2 x 1). Doing this by hand means multiplying all numbers one by one, which can get tricky and slow for bigger numbers.
Manually multiplying each number is slow and easy to mess up, especially if the number is large. You might forget a number or multiply in the wrong order, causing mistakes.
Recursion lets the computer break the problem into smaller, similar problems. It solves the factorial by calling itself with smaller numbers until it reaches 1, making the process simple and error-free.
function factorial(number) {
let result = 1;
for (let i = number; i > 0; i--) {
result *= i;
}
return result;
}function factorial(number) {
if (number <= 1) return 1;
return number * factorial(number - 1);
}Recursion enables solving complex problems by breaking them down into simpler, repeatable steps automatically.
Calculating combinations in probability, where factorials are used to find how many ways to arrange or select items.
Manual multiplication is slow and error-prone for factorials.
Recursion breaks the problem into smaller, easier steps.
Recursive factorial is simpler and less error-prone.
Practice
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.Step 2: Compare options with definition
Only The product of all positive integers up to that number matches this definition exactly.Final Answer:
The product of all positive integers up to that number -> Option CQuick Check:
Factorial = product sequence [OK]
- Confusing factorial with sum
- Thinking factorial doubles the number
- Mixing factorial with division
Solution
Step 1: Identify base case for factorial
Factorial of 0 is defined as 1, so base case should return 1 when n is 0.Step 2: Check options for correct base case
if (n === 0) return 1; returns 1 when n is 0, which is correct. Others either return wrong values or recursive calls.Final Answer:
if (n === 0) return 1; -> Option DQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for factorial(0)
- Using recursive call in base case
- Confusing base case with recursive step
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));Solution
Step 1: Trace recursive calls for factorial(4)
factorial(4) = 4 * factorial(3) factorial(3) = 3 * factorial(2) factorial(2) = 2 * factorial(1) factorial(1) = 1 * factorial(0) factorial(0) = 1 (base case)Step 2: Calculate values bottom-up
factorial(1) = 1 * 1 = 1 factorial(2) = 2 * 1 = 2 factorial(3) = 3 * 2 = 6 factorial(4) = 4 * 6 = 24Final Answer:
24 -> Option AQuick Check:
4! = 24 [OK]
- Adding instead of multiplying
- Stopping recursion too early
- Misunderstanding base case
function factorial(n) {
if (n === 1) return 1;
return n * factorial(n + 1);
}Solution
Step 1: Analyze recursive call direction
The function calls factorial(n + 1), which increases n, causing infinite recursion.Step 2: Correct recursive call
To reach base case, n should decrease by 1 each call: factorial(n - 1).Final Answer:
The recursive call should use n - 1, not n + 1 -> Option BQuick Check:
Recursion must move toward base case [OK]
- Increasing n in recursive call
- Wrong base case value
- Using addition instead of multiplication
function evenFactorial(n) {
if (n === 0) return 1;
if (n % 2 !== 0) return 0;
return n * evenFactorial(n - 2);
}
console.log(evenFactorial(6));What is the output of this code?
Solution
Step 1: Understand function behavior
Function returns 0 if n is odd. For even n, it multiplies n by factorial of n-2 recursively.Step 2: Calculate evenFactorial(6)
evenFactorial(6) = 6 * evenFactorial(4) evenFactorial(4) = 4 * evenFactorial(2) evenFactorial(2) = 2 * evenFactorial(0) evenFactorial(0) = 1 (base case) So, 2 * 1 = 2 4 * 2 = 8 6 * 8 = 48Final Answer:
48 -> Option AQuick Check:
Product of even numbers down to 0 = 48 [OK]
- Calculating full factorial instead of even factorial
- Returning 0 for even inputs
- Ignoring base case for 0
Ready to prove it?
Pass the test and this pattern moves unknown
