-
-
Notifications
You must be signed in to change notification settings - Fork 636
Update recursion concept documentation #2348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zynthatrix
wants to merge
4
commits into
exercism:main
Choose a base branch
from
zynthatrix:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 should have at least one base case, a condition where the function stops calling itself. | ||
zynthatrix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
zynthatrix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.