Skip to content

Commit d8f5c6a

Browse files
feat(curriculum): add interactive examples to recursion and call stack lesson (freeCodeCamp#63679)
Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1 parent 2d15487 commit d8f5c6a

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

curriculum/challenges/english/blocks/lecture-understanding-recursion-and-the-call-stack/6733b02d1e556005a544c5e3.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,69 @@ challengeType: 19
55
dashedName: what-is-recursion-and-how-does-it-work
66
---
77

8-
# --description--
9-
10-
Let's learn how recursion works in JavaScript.
8+
# --interactive--
119

1210
Recursion is a complicated feature that allows you to call a function repeatedly until a base-case is reached. Unlike a traditional loop, recursion allows you to handle something with an unknown depth, such as deeply nested objects/arrays, or a file tree. But you can also use it for more basic tasks, such as counting down from a given number.
1311

1412
Let's construct a function to do exactly that. We’ll call our function `recursiveCountdown`, and it needs to accept a number. We’ll have it print this number to the console:
1513

14+
:::interactive_editor
15+
1616
```js
1717
const recursiveCountdown = (number) => {
1818
console.log(number);
1919
};
20+
21+
recursiveCountdown(5);
2022
```
2123

24+
:::
25+
2226
Now if we call this and pass the number 5, we’ll see the number print to our terminal. But nothing else happens – and the number 5 certainly isn’t a countdown.
2327

2428
Before we start building the recursive portion of our function, we need to establish our base case first. If you don’t have a base case established, your code will run until it exceeds your memory allocation and crashes.
2529

30+
:::interactive_editor
31+
2632
```js
2733
const recursiveCountdown = (number) => {
28-
if (number < 1) {
29-
return;
30-
}
31-
console.log(number);
32-
};
34+
if (number < 1) {
35+
return;
36+
}
37+
console.log(number);
38+
};
3339

3440
recursiveCountdown(5);
3541
```
3642

43+
:::
44+
3745
For our base case, we want the countdown to stop if the number is less than 1. When we hit that base-case, we can return to break out of the function execution.
3846

3947
Now that we’ve safely prepared a base-case, we can set up the recursion. The key point that makes a function recursive is that it calls itself in its execution. In this case, we want to call the function after we print the number. But in order to count down, our new number needs to be one less:
4048

49+
:::interactive_editor
50+
4151
```js
4252
const recursiveCountdown = (number) => {
43-
if (number < 1) {
44-
return;
45-
}
46-
console.log(number);
47-
recursiveCountdown(number - 1);
48-
};
53+
if (number < 1) {
54+
return;
55+
}
56+
console.log(number);
57+
recursiveCountdown(number - 1);
58+
};
4959

50-
recursiveCountdown(5); //
60+
recursiveCountdown(5);
5161
```
5262

63+
:::
64+
5365
This would log the numbers 5, 4, 3, 2, and 1 to the console.
5466

5567
We do get our five numbers! But what if we wanted to count up instead? Rather than writing an entirely new function, we can swap the order of our log and our recursive call:
5668

69+
:::interactive_editor
70+
5771
```js
5872
const recursiveCountdown = (number) => {
5973
if (number < 1) {
@@ -66,25 +80,31 @@ const recursiveCountdown = (number) => {
6680
recursiveCountdown(5);
6781
```
6882

83+
:::
84+
6985
This would log the numbers 1, 2, 3, 4, and 5 to the console.
7086

7187
But why does that work? Well, to understand this you need to understand the call stack. The call stack is how JavaScript tracks and resolves function calls. The stack functions as a last-in-first-out queue of sorts. To understand this better, let’s add some logging to our function:
7288

89+
:::interactive_editor
90+
7391
```js
7492
const recursiveCountdown = (number) => {
75-
console.log(`Function execution started for number: ${number}`);
76-
if (number < 1) {
77-
console.log(`Base case reached, begin resolving stack`);
78-
return;
79-
}
80-
console.log(`Calling recursiveCountdown with number: ${number - 1}`);
81-
recursiveCountdown(number - 1);
82-
console.log(`Function execution completed for number: ${number}`);
83-
};
93+
console.log(`Function execution started for number: ${number}`);
94+
if (number < 1) {
95+
console.log(`Base case reached, begin resolving stack`);
96+
return;
97+
}
98+
console.log(`Calling recursiveCountdown with number: ${number - 1}`);
99+
recursiveCountdown(number - 1);
100+
console.log(`Function execution completed for number: ${number}`);
101+
};
84102

85103
recursiveCountdown(5);
86104
```
87105

106+
:::
107+
88108
We’ve added four key statements here. The first log runs when a function call begins executing. The third log runs just before the recursive function is called. And the fourth log runs when the function execution has ended. The result is:
89109

90110
```md

0 commit comments

Comments
 (0)