📖 Scenario: You are building a simple calculator that can find the factorial of a number. Factorial means multiplying a number by all the numbers below it down to 1. For example, factorial of 4 is 4 x 3 x 2 x 1 = 24.
🎯 Goal: Write a recursive function in JavaScript to calculate the factorial of a given number.
📋 What You'll Learn
Create a variable called number with the value 5
Create a recursive function called factorial that takes one parameter n
The function should return 1 if n is 0 or 1
Otherwise, the function should return n multiplied by factorial(n - 1)
Print the result of factorial(number)
💡 Why This Matters
🌍 Real World
Factorials are used in math, statistics, and computer science for counting combinations and permutations.
💼 Career
Understanding recursion and factorials helps in algorithm design and problem solving in software development.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 5.
DSA Javascript
Hint
Use const number = 5; to create the variable.
2
Define the recursive factorial function
Create a function called factorial that takes one parameter n. If n is 0 or 1, return 1. Otherwise, return n * factorial(n - 1).
DSA Javascript
Hint
Use an if statement to check if n is 0 or 1, then return 1. Otherwise, return n * factorial(n - 1).
3
Call the factorial function
Create a variable called result and set it to the value of factorial(number).
DSA Javascript
Hint
Call the function with number and store the answer in result.
4
Print the factorial result
Print the value of result using console.log.
DSA Javascript
Hint
Use console.log(result); to print the factorial.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The number multiplied by 2
B. The sum of all positive integers up to that number
C. The product of all positive integers up to that number
D. The number divided by 2
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 C
Quick Check:
Factorial = product sequence [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial doubles the number
Mixing factorial with division
2. Which of the following is the correct base case for a recursive factorial function in JavaScript?
easy
A. if (n === 1) return n * factorial(n - 1);
B. if (n === 1) return 0;
C. if (n === 0) return 0;
D. if (n === 0) return 1;
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 D
Quick Check:
Base case n=0 returns 1 [OK]
Hint: Base case for factorial(0) is 1 [OK]
Common Mistakes:
Returning 0 for factorial(0)
Using recursive call in base case
Confusing base case with recursive step
3. What will be the output of the following JavaScript code?
function factorial(n) {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));