diff --git a/concepts/recursion/about.md b/concepts/recursion/about.md index 4b51d644bd..aa3e11251d 100644 --- a/concepts/recursion/about.md +++ b/concepts/recursion/about.md @@ -1 +1,93 @@ -# About +# Understanding Recursion in JavaScript + +Recursion is a powerful concept in programming that involves a function calling itself. +It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems. +In this tutorial, we'll explore recursion in JavaScript with easy-to-understand examples. + +## What is Recursion? + +Recursion occurs when a function calls itself, either directly or indirectly. +It's similar to a loop, but it may involve breaking a problem down into smaller, more manageable sub-problems. + +### Example 1: Countdown + +Let's start with a simple example: a countdown function. + +```javascript +function countdown(num) { + // Base case + if (num <= 0) { + console.log("Blastoff!"); + return; + } + + // Recursive case + console.log(num); + countdown(num - 1); +} + +// Call the function +countdown(5); +``` + +In this example: + +- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself. +- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`. + +### Example 2: Factorial + +Now, let's look at a classic example of recursion: calculating the factorial of a number. + +```javascript +function factorial(n) { + // Base case + if (n === 0 || n === 1) { + return 1; + } + + // Recursive case + return n * factorial(n - 1); +} + +// Test the function +console.log(factorial(5)); // Output: 120 +``` + +In this example: + +- **Base case**: When `n` is 0 or 1, the function returns 1. +- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`. + +## Key Concepts + +### Base Case + +Every recursive function should have at least one base case, a condition where the function stops calling itself. +Without a base case, the recursion would continue indefinitely, leading to a stack overflow. + +### Recursive Case + +The recursive case defines how the function calls itself with a smaller or simpler version of the problem. + +## Pros and Cons of Recursion + +**Pros:** + +- Elegant solution for certain problems. +- Mimics the mathematical induction concept. + +**Cons:** + +- Can be less efficient than iterative solutions. +- May lead to stack overflow for deep recursion. + +## Conclusion + +Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems. +Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript. + +**Learn More:** + +- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion) +- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html) diff --git a/concepts/recursion/introduction.md b/concepts/recursion/introduction.md index 83d31dab92..d36865affc 100644 --- a/concepts/recursion/introduction.md +++ b/concepts/recursion/introduction.md @@ -1,3 +1,93 @@ -# Introduction +# Understanding Recursion in JavaScript -TODO: add introduction for recursion concept +Recursion is a powerful concept in programming that involves a function calling itself. +It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems. +In this tutorial, we'll explore recursion in JavaScript with easy-to-understand examples. + +## What is Recursion? + +Recursion occurs when a function calls itself, either directly or indirectly. +It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems. + +### Example 1: Countdown + +Let's start with a simple example: a countdown function. + +```javascript +function countdown(num) { + // Base case + if (num <= 0) { + console.log("Blastoff!"); + return; + } + + // Recursive case + console.log(num); + countdown(num - 1); +} + +// Call the function +countdown(5); +``` + +In this example: + +- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself. +- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`. + +### Example 2: Factorial + +Now, let's look at a classic example of recursion: calculating the factorial of a number. + +```javascript +function factorial(n) { + // Base case + if (n === 0 || n === 1) { + return 1; + } + + // Recursive case + return n * factorial(n - 1); +} + +// Test the function +console.log(factorial(5)); // Output: 120 +``` + +In this example: + +- **Base case**: When `n` is 0 or 1, the function returns 1. +- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`. + +## Key Concepts + +### Base Case + +Every recursive function should have at least one base case, a condition where the function stops calling itself. +Without a base case, the recursion would continue indefinitely, leading to a stack overflow. + +### Recursive Case + +The recursive case defines how the function calls itself with a smaller or simpler version of the problem. + +## Pros and Cons of Recursion + +**Pros:** + +- Elegant solution for certain problems. +- Mimics the mathematical induction concept. + +**Cons:** + +- Can be less efficient than iterative solutions. +- May lead to stack overflow for deep recursion. + +## Conclusion + +Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems. +Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript. + +**Learn More:** + +- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion) +- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html) diff --git a/exercises/concept/pizza-order/.docs/introduction.md b/exercises/concept/pizza-order/.docs/introduction.md index 28571cd59a..0d05746fb8 100644 --- a/exercises/concept/pizza-order/.docs/introduction.md +++ b/exercises/concept/pizza-order/.docs/introduction.md @@ -1,73 +1,93 @@ -# Introduction +# Understanding Recursion in JavaScript -The ability for something to be defined in terms of itself is called recursion. -Recursive functions are functions that call themselves. +Recursion is a powerful concept in programming that involves a function calling itself. +It can be a bit tricky to grasp at first, but once you understand the fundamentals, it becomes a valuable tool in solving complex problems. +In this tutorial, we'll explore recursion in JavaScript with easy-to-understand examples. -Suppose that you have a function called `recurse`. -This function is recursive if it calls itself inside its body, like this: +## What is Recursion? -```js -function recurse() { - // ... - recurse(); - // ... -} -``` +Recursion occurs when a function calls itself, either directly or indirectly. +It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems. -A recursive function usually has a condition to stop calling itself and return a value, known as a _base case_. -If a base case is missing, in most cases, because it will call itself indefinitely, it would be able to run forever. -In reality, in most of those situations, you'll end up with a "StackSize error": an error raised by the runtime because the _stack_ of function calls has grown beyond a predefined limit because each recursive call adds to this _stack_ until it returns (and it doesn't). -The message of this error is `Maximum call stack size exceeded`. - -```js -function recurse() { - if (baseCondition) { - // stop calling itself - //... - } else { - recurse(); - } -} -``` +### Example 1: Countdown -Recursive functions often can be used instead of `for` loops for more succinct code. -For example, take a countdown. -Here's the more intuitive `for` loop approach: +Let's start with a simple example: a countdown function. -```js -function countDown(fromNumber) { - for (let i = fromNumber; i > 0; i--) { - console.log(i); +```javascript +function countdown(num) { + // Base case + if (num <= 0) { + console.log("Blastoff!"); + return; } + + // Recursive case + console.log(num); + countdown(num - 1); } -countDown(3); // 3, 2, 1 in separate lines +// Call the function +countdown(5); ``` -We could solve this using recursion too: +In this example: + +- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself. +- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`. -```js -function countDown(fromNumber) { - console.log(fromNumber); - if (fromNumber > 1) { - countDown(fromNumber - 1); +### Example 2: Factorial + +Now, let's look at a classic example of recursion: calculating the factorial of a number. + +```javascript +function factorial(n) { + // Base case + if (n === 0 || n === 1) { + return 1; } + + // Recursive case + return n * factorial(n - 1); } -countDown(3); // same result +// Test the function +console.log(factorial(5)); // Output: 120 ``` -Here, our base case is when `fromNumber` is 1, in which case we don't call `countDown` again. +In this example: -Apart from just displaying numbers, recursive functions can be used for more complicated procedures, such as keeping a sum or total. +- **Base case**: When `n` is 0 or 1, the function returns 1. +- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`. -```js -function sum(n) { - if (n <= 1) { - return n; - } - return n + sum(n - 1); -} +## Key Concepts -sum(3); // 6 -``` +### Base Case + +Every recursive function must have at least one base case, a condition where the function stops calling itself. +Without a base case, the recursion would continue indefinitely, leading to a stack overflow. + +### Recursive Case + +The recursive case defines how the function calls itself with a smaller or simpler version of the problem. + +## Pros and Cons of Recursion + +**Pros:** + +- Elegant solution for certain problems. +- Mimics the mathematical induction concept. + +**Cons:** + +- Can be less efficient than iterative solutions. +- May lead to stack overflow for deep recursion. + +## Conclusion + +Recursion is a valuable technique that can simplify complex problems by breaking them into smaller, more manageable sub-problems. +Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript. + +**Learn More:** + +- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion) +- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)