📖 Scenario: Imagine you are helping a friend who wants to calculate the factorial of a number. Factorial means multiplying a number by all the numbers below it down to 1. For example, 4 factorial is 4 x 3 x 2 x 1 = 24.
🎯 Goal: You will write a small program in TypeScript that uses a function calling itself (recursion) to find the factorial of a number.
📋 What You'll Learn
Create a variable called num with the value 5
Create a function called factorial that uses recursion to calculate factorial
Call the factorial function with num and store the result in result
Print the value of result
💡 Why This Matters
🌍 Real World
Calculating factorials is useful in math problems, probability, and computer science algorithms.
💼 Career
Understanding recursion helps in solving complex problems and is a common topic in programming interviews.
Progress0 / 4 steps
1
Create the number variable
Create a variable called num and set it to the number 5.
DSA Typescript
Hint
Use const num = 5; to create the number variable.
2
Write the recursive factorial function
Create a function called factorial that takes a parameter n. Use recursion: if n is 1, return 1; otherwise return n * factorial(n - 1).
DSA Typescript
Hint
Remember, the function calls itself with n - 1 until it reaches 1.
3
Calculate factorial and store result
Call the factorial function with num and store the result in a variable called result.
DSA Typescript
Hint
Use const result = factorial(num); to store the factorial.
4
Print the factorial result
Print the value of result using console.log.
DSA Typescript
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 product of all positive integers up to that number
B. The sum of all positive integers up to that number
C. The number multiplied by 2
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
Only the product of all positive integers up to that number correctly describes factorial as a product, not sum or other operations.
Final Answer:
The product of all positive integers up to that number -> Option A
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial means multiply all numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial divides
Mixing factorial with doubling
2. Which of the following is the correct base case for a recursive factorial function in TypeScript?
easy
A. if (n === 0) return 1;
B. if (n === 1) return 0;
C. if (n === 0) return 0;
D. if (n === 1) return n + 1;
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so the base case should return 1 when n is 0.
Step 2: Check options
Only if (n === 0) return 1; returns 1 when n is 0, which is correct base case for recursion.
Final Answer:
if (n === 0) return 1; -> Option A
Quick Check:
Base case: 0! = 1 [OK]
Hint: Base case for factorial is 0 returns 1 [OK]
Common Mistakes:
Returning 0 for base case
Using wrong base condition
Returning n+1 incorrectly
3. What is the output of this TypeScript code?
function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));