Day 10 – Introduction to Recursion


What is Recursion?
Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem.
A recursive function has two main parts:
Base Case – The condition where the recursion stops.
Recursive Case – The part where the function calls itself.
Why Use Recursion?
Simplifies problems that can be broken into smaller, similar sub-problems.
Often used for problems like:
Factorials
Fibonacci series
Tree and graph traversals
Divide and Conquer algorithms (e.g., Merge Sort, Quick Sort)
Basic Structure of a Recursive Function
function recursiveFunction(parameters) {
if (baseCaseCondition) {
return result; // Base case
} else {
// Recursive case
return recursiveFunction(modifiedParameters);
}
}
Example 1 – Factorial Using Recursion
Mathematically:
n! = n × (n-1)! and 0! = 1
function factorial(n) {
if (n === 0) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive case
}
console.log(factorial(5)); // Output: 120
Example 2 – Fibonacci Series Using Recursion
function fibonacci(n) {
if (n <= 1) { // Base cases
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive calls
}
console.log(fibonacci(6)); // Output: 8
Key Things to Remember
✅ Always have a base case to avoid infinite recursion.
✅ Each recursive call should bring the problem closer to the base case.
✅ Recursion can be memory-heavy for large inputs (stack overflow risk).
Practice Tasks
Write a recursive function to calculate the sum of numbers from 1 to n.
Write a recursive function to reverse a string.
Write a recursive function to find the greatest common divisor (GCD) of two numbers.
#CodeWithGift #DSA2025 #JavaScript #DayTen
Subscribe to my newsletter
Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

God'sgift Samuel
God'sgift Samuel
About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.