📖 Scenario: Imagine you are stacking boxes one on top of another. To find out how many boxes are stacked, you can count one box and then ask the smaller stack below to count itself. This is like recursion, where a problem is solved by solving smaller versions of the same problem.
🎯 Goal: You will build a simple recursive function in TypeScript that counts down from a given number to 1, printing each step. This will help you see how recursion works and how the call stack grows and shrinks.
📋 What You'll Learn
Create a variable called startNumber with the value 5
Create a recursive function called countDown that takes a number parameter n
Inside countDown, print the current number n
Make countDown call itself with n - 1 if n is greater than 1
Call countDown with startNumber
Print each number on its own line
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching files in folders, solving puzzles, and processing nested data.
💼 Career
Understanding recursion helps in software development roles that involve algorithms, data processing, and problem solving.
Progress0 / 4 steps
1
Create the starting number
Create a variable called startNumber and set it to 5.
DSA Typescript
Hint
Use const startNumber = 5; to create the variable.
2
Define the recursive function
Create a function called countDown that takes a parameter n of type number.
DSA Typescript
Hint
Write function countDown(n: number): void { } to define the function.
3
Add the recursive logic
Inside countDown, print the current number n using console.log(n). Then, if n is greater than 1, call countDown again with n - 1.
DSA Typescript
Hint
Use console.log(n); to print and if (n > 1) { countDown(n - 1); } for recursion.
4
Call the function and see the output
Call the function countDown with the variable startNumber to start the countdown and print the numbers.
DSA Typescript
Hint
Write countDown(startNumber); to start the countdown.
Practice
(1/5)
1. What happens when a recursive function calls itself without a stopping condition?
easy
A. It runs only once and stops
B. It causes an infinite loop leading to a stack overflow error
C. It immediately returns without doing anything
D. It throws a syntax error
Solution
Step 1: Understand recursion stopping condition
A recursive function must have a base case to stop calling itself; otherwise, it keeps calling endlessly.
Step 2: Effect of missing stopping condition
Without a stopping condition, the function calls stack up until memory runs out, causing a stack overflow error.
Final Answer:
It causes an infinite loop leading to a stack overflow error -> Option B
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Assuming recursion stops automatically
Confusing infinite loop with syntax error
Thinking recursion runs only once without base case
2. Which of the following is the correct syntax to define a recursive function in TypeScript?
easy
A. function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); }
B. function recurse(n: number) { while(n > 0) recurse(n - 1); }
C. function recurse(n: number): number { return n + recurse(n + 1); }
D. function recurse(n: number): number { return n * recurse(n); }
Solution
Step 1: Check for base case and recursive call
function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); } has a base case (n <= 0) and calls itself with n - 1, which is correct for recursion.
Step 2: Analyze other options for errors
The version with a while loop lacks a proper base case and return statement; another calls recurse with n + 1 causing infinite recursion; another calls recurse with the same n causing infinite recursion.
Final Answer:
function recurse(n: number): number { if (n <= 0) return 0; return n + recurse(n - 1); } -> Option A
Quick Check:
Base case + decreasing argument = correct recursion [OK]
Hint: Look for base case and argument reducing in recursion [OK]
Common Mistakes:
Missing base case
Increasing argument causing infinite recursion
Using loops instead of recursion
3. What is the output of the following TypeScript code?
function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(factorial(3));