Skip to content

Commit 1db5b93

Browse files
spool6kotpSleeplessByte
authored
Update recursion concept documentation (#2348)
* Update recursion concept documentation * Update about.md Co-authored-by: Victor Goff <[email protected]> * Update introduction.md Co-authored-by: Victor Goff <[email protected]> * Update introduction.md Co-authored-by: Victor Goff <[email protected]> * Add author @zynthatrix, format, and re-replace intro.md * Format file --------- Co-authored-by: Jebrich <[email protected]> Co-authored-by: Victor Goff <[email protected]> Co-authored-by: Derk-Jan Karrenbeld <[email protected]>
1 parent 63b3d60 commit 1db5b93

File tree

4 files changed

+240
-108
lines changed

4 files changed

+240
-108
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"blurb": "Recursive functions are functions that call themselves.",
3-
"authors": ["SleeplessByte"],
3+
"authors": ["SleeplessByte", "zynthatrix"],
44
"contributors": []
55
}

concepts/recursion/about.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
# About
1+
# Understanding Recursion in JavaScript
2+
3+
Recursion is a powerful concept in programming that involves a function calling itself.
4+
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.
5+
We'll explore recursion in JavaScript with easy-to-understand examples.
6+
7+
## What is Recursion?
8+
9+
Recursion occurs when a function calls itself, either directly or indirectly.
10+
It's similar to a loop, but it may involve breaking a problem down into smaller, more manageable sub-problems.
11+
12+
### Example 1: Countdown
13+
14+
Let's start with a simple example: a countdown function.
15+
16+
```javascript
17+
function countdown(num) {
18+
// Base case
19+
if (num <= 0) {
20+
console.log('Blastoff!');
21+
return;
22+
}
23+
24+
// Recursive case
25+
console.log(num);
26+
countdown(num - 1);
27+
}
28+
29+
// Call the function
30+
countdown(5);
31+
```
32+
33+
In this example:
34+
35+
- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
36+
- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`.
37+
38+
### Example 2: Factorial
39+
40+
Now, let's look at a classic example of recursion: calculating the factorial of a number.
41+
42+
```javascript
43+
function factorial(n) {
44+
// Base case
45+
if (n === 0 || n === 1) {
46+
return 1;
47+
}
48+
49+
// Recursive case
50+
return n * factorial(n - 1);
51+
}
52+
53+
// Test the function
54+
console.log(factorial(5)); // Output: 120
55+
```
56+
57+
In this example:
58+
59+
- **Base case**: When `n` is 0 or 1, the function returns 1.
60+
- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`.
61+
62+
## Key Concepts
63+
64+
### Base Case
65+
66+
Every recursive function should have at least one base case, a condition where the function stops calling itself.
67+
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
68+
69+
### Recursive Case
70+
71+
The recursive case defines how the function calls itself with a smaller or simpler version of the problem.
72+
73+
## Pros and Cons of Recursion
74+
75+
**Pros:**
76+
77+
- Elegant solution for certain problems.
78+
- Mimics the mathematical induction concept.
79+
80+
**Cons:**
81+
82+
- Can be less efficient than iterative solutions.
83+
- May lead to stack overflow for deep recursion.
84+
85+
## Conclusion
86+
87+
Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems.
88+
Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.
89+
90+
**Learn More:**
91+
92+
- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion)
93+
- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)

concepts/recursion/introduction.md

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,93 @@
1-
# Introduction
1+
# Understanding Recursion in JavaScript
22

3-
The ability for something to be defined in terms of itself is called recursion.
4-
Recursive functions are functions that call themselves.
3+
Recursion is a powerful concept in programming that involves a function calling itself.
4+
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.
5+
We'll explore recursion in JavaScript with easy-to-understand examples.
56

6-
Suppose that you have a function called `recurse`.
7-
This function is recursive if it calls itself inside its body, like this:
7+
## What is Recursion?
88

9-
```js
10-
function recurse() {
11-
// ...
12-
recurse();
13-
// ...
14-
}
15-
```
9+
Recursion occurs when a function calls itself, either directly or indirectly.
10+
It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems.
1611

17-
A recursive function usually has a condition to stop calling itself and return a value, known as a _base case_.
18-
If a base case is missing, in most cases, because it will call itself indefinitely, it would be able to run forever.
19-
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).
20-
The message of this error is `Maximum call stack size exceeded`.
21-
22-
```js
23-
function recurse() {
24-
if (baseCondition) {
25-
// stop calling itself
26-
//...
27-
} else {
28-
recurse();
29-
}
30-
}
31-
```
12+
### Example 1: Countdown
3213

33-
Recursive functions often can be used instead of `for` loops for more succinct code.
34-
For example, take a countdown.
35-
Here's the more intuitive `for` loop approach:
14+
Let's start with a simple example: a countdown function.
3615

37-
```js
38-
function countDown(fromNumber) {
39-
for (let i = fromNumber; i > 0; i--) {
40-
console.log(i);
16+
```javascript
17+
function countdown(num) {
18+
// Base case
19+
if (num <= 0) {
20+
console.log('Blastoff!');
21+
return;
4122
}
23+
24+
// Recursive case
25+
console.log(num);
26+
countdown(num - 1);
4227
}
4328

44-
countDown(3); // 3, 2, 1 in separate lines
29+
// Call the function
30+
countdown(5);
4531
```
4632

47-
We could solve this using recursion too:
33+
In this example:
34+
35+
- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
36+
- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`.
4837

49-
```js
50-
function countDown(fromNumber) {
51-
console.log(fromNumber);
52-
if (fromNumber > 1) {
53-
countDown(fromNumber - 1);
38+
### Example 2: Factorial
39+
40+
Now, let's look at a classic example of recursion: calculating the factorial of a number.
41+
42+
```javascript
43+
function factorial(n) {
44+
// Base case
45+
if (n === 0 || n === 1) {
46+
return 1;
5447
}
48+
49+
// Recursive case
50+
return n * factorial(n - 1);
5551
}
5652

57-
countDown(3); // same result
53+
// Test the function
54+
console.log(factorial(5)); // Output: 120
5855
```
5956

60-
Here, our base case is when `fromNumber` is 1, in which case we don't call `countDown` again.
57+
In this example:
6158

62-
Apart from just displaying numbers, recursive functions can be used for more complicated procedures, such as keeping a sum or total.
59+
- **Base case**: When `n` is 0 or 1, the function returns 1.
60+
- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`.
6361

64-
```js
65-
function sum(n) {
66-
if (n <= 1) {
67-
return n;
68-
}
69-
return n + sum(n - 1);
70-
}
62+
## Key Concepts
7163

72-
sum(3); // 6
73-
```
64+
### Base Case
65+
66+
Every recursive function should have at least one base case, a condition where the function stops calling itself.
67+
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
68+
69+
### Recursive Case
70+
71+
The recursive case defines how the function calls itself with a smaller or simpler version of the problem.
72+
73+
## Pros and Cons of Recursion
74+
75+
**Pros:**
76+
77+
- Elegant solution for certain problems.
78+
- Mimics the mathematical induction concept.
79+
80+
**Cons:**
81+
82+
- Can be less efficient than iterative solutions.
83+
- May lead to stack overflow for deep recursion.
84+
85+
## Conclusion
86+
87+
Recursion is a valuable technique that simplifies complex problems by breaking them into smaller, more manageable sub-problems.
88+
Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.
89+
90+
**Learn More:**
91+
92+
- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion)
93+
- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)
Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,93 @@
1-
# Introduction
1+
# Understanding Recursion in JavaScript
22

3-
The ability for something to be defined in terms of itself is called recursion.
4-
Recursive functions are functions that call themselves.
3+
Recursion is a powerful concept in programming that involves a function calling itself.
4+
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.
5+
In this tutorial, we'll explore recursion in JavaScript with easy-to-understand examples.
56

6-
Suppose that you have a function called `recurse`.
7-
This function is recursive if it calls itself inside its body, like this:
7+
## What is Recursion?
88

9-
```js
10-
function recurse() {
11-
// ...
12-
recurse();
13-
// ...
14-
}
15-
```
9+
Recursion occurs when a function calls itself, either directly or indirectly.
10+
It's similar to a loop, but it involves breaking a problem down into smaller, more manageable sub-problems.
1611

17-
A recursive function usually has a condition to stop calling itself and return a value, known as a _base case_.
18-
If a base case is missing, in most cases, because it will call itself indefinitely, it would be able to run forever.
19-
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).
20-
The message of this error is `Maximum call stack size exceeded`.
21-
22-
```js
23-
function recurse() {
24-
if (baseCondition) {
25-
// stop calling itself
26-
//...
27-
} else {
28-
recurse();
29-
}
30-
}
31-
```
12+
### Example 1: Countdown
3213

33-
Recursive functions often can be used instead of `for` loops for more succinct code.
34-
For example, take a countdown.
35-
Here's the more intuitive `for` loop approach:
14+
Let's start with a simple example: a countdown function.
3615

37-
```js
38-
function countDown(fromNumber) {
39-
for (let i = fromNumber; i > 0; i--) {
40-
console.log(i);
16+
```javascript
17+
function countdown(num) {
18+
// Base case
19+
if (num <= 0) {
20+
console.log('Blastoff!');
21+
return;
4122
}
23+
24+
// Recursive case
25+
console.log(num);
26+
countdown(num - 1);
4227
}
4328

44-
countDown(3); // 3, 2, 1 in separate lines
29+
// Call the function
30+
countdown(5);
4531
```
4632

47-
We could solve this using recursion too:
33+
In this example:
34+
35+
- **Base case**: When `num` becomes less than or equal to 0, the function prints "Blastoff!" and stops calling itself.
36+
- **Recursive case**: The function prints the current `num` and calls itself with `num - 1`.
4837

49-
```js
50-
function countDown(fromNumber) {
51-
console.log(fromNumber);
52-
if (fromNumber > 1) {
53-
countDown(fromNumber - 1);
38+
### Example 2: Factorial
39+
40+
Now, let's look at a classic example of recursion: calculating the factorial of a number.
41+
42+
```javascript
43+
function factorial(n) {
44+
// Base case
45+
if (n === 0 || n === 1) {
46+
return 1;
5447
}
48+
49+
// Recursive case
50+
return n * factorial(n - 1);
5551
}
5652

57-
countDown(3); // same result
53+
// Test the function
54+
console.log(factorial(5)); // Output: 120
5855
```
5956

60-
Here, our base case is when `fromNumber` is 1, in which case we don't call `countDown` again.
57+
In this example:
6158

62-
Apart from just displaying numbers, recursive functions can be used for more complicated procedures, such as keeping a sum or total.
59+
- **Base case**: When `n` is 0 or 1, the function returns 1.
60+
- **Recursive case**: The function multiplies `n` by the factorial of `n - 1`.
6361

64-
```js
65-
function sum(n) {
66-
if (n <= 1) {
67-
return n;
68-
}
69-
return n + sum(n - 1);
70-
}
62+
## Key Concepts
7163

72-
sum(3); // 6
73-
```
64+
### Base Case
65+
66+
Every recursive function must have at least one base case, a condition where the function stops calling itself.
67+
Without a base case, the recursion would continue indefinitely, leading to a stack overflow.
68+
69+
### Recursive Case
70+
71+
The recursive case defines how the function calls itself with a smaller or simpler version of the problem.
72+
73+
## Pros and Cons of Recursion
74+
75+
**Pros:**
76+
77+
- Elegant solution for certain problems.
78+
- Mimics the mathematical induction concept.
79+
80+
**Cons:**
81+
82+
- Can be less efficient than iterative solutions.
83+
- May lead to stack overflow for deep recursion.
84+
85+
## Conclusion
86+
87+
Recursion is a valuable technique that can simplify complex problems by breaking them into smaller, more manageable sub-problems.
88+
Understanding base cases and recursive cases is crucial for implementing effective recursive solutions in JavaScript.
89+
90+
**Learn More:**
91+
92+
- [MDN: Recursion in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#recursion)
93+
- [Eloquent JavaScript: Chapter 3 - Functions](https://eloquentjavascript.net/03_functions.html)

0 commit comments

Comments
 (0)