You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/article.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ There are two ways to implement it.
31
31
let result =1;
32
32
33
33
// multiply result by x n times in the loop
34
-
for(let i =0; i < n; i++) {
34
+
for(let i =0; i < n; i++) {
35
35
result *= x;
36
36
}
37
37
@@ -67,8 +67,8 @@ pow(x, n) =
67
67
else = x * pow(x, n - 1)
68
68
```
69
69
70
-
1. If `n==1`, then everything is trivial. It is called *the base*of recursion, because it immediately produces the obvious result:`pow(x, 1)` equals `x`.
71
-
2. Otherwise, we can represent `pow(x, n)` as `x * pow(x, n-1)`. In maths, one would write <code>x<sup>n</sup>= x * x<sup>n-1</sup></code>. This is called *a recursive step*: we transform the task into a simpler action (multiplication by `x`) and a simpler call of the same task (`pow`with lower `n`). Next steps simplify it further and further untill`n` reaches `1`.
70
+
1. If `n == 1`, then everything is trivial. It is called *the base*of recursion, because it immediately produces the obvious result:`pow(x, 1)` equals `x`.
71
+
2. Otherwise, we can represent `pow(x, n)` as `x * pow(x, n - 1)`. In maths, one would write <code>x<sup>n</sup>= x * x<sup>n-1</sup></code>. This is called *a recursive step*: we transform the task into a simpler action (multiplication by `x`) and a simpler call of the same task (`pow`with lower `n`). Next steps simplify it further and further until`n` reaches `1`.
72
72
73
73
We can also say that `pow`*recursively calls itself* till `n == 1`.
74
74
@@ -91,7 +91,7 @@ Here we can rewrite the same using the ternary `?` operator instead of `if` to m
91
91
92
92
```js run
93
93
function pow(x, n) {
94
-
return (n ==1) ? x : (x *pow(x, n-1));
94
+
return (n ==1) ? x : (x *pow(x, n-1));
95
95
}
96
96
```
97
97
````
@@ -134,7 +134,7 @@ We can sketch it as:
134
134
</li>
135
135
</ul>
136
136
137
-
That's when the function starts to execute. The condition `n == 1` is falsy, so the flow continues into the second branch of `if`:
137
+
That's when the function starts to execute. The condition `n == 1` is false, so the flow continues into the second branch of `if`:
138
138
139
139
```js run
140
140
function pow(x, n) {
@@ -160,7 +160,7 @@ The variables are same, but the line changes, so the context is now:
160
160
</li>
161
161
</ul>
162
162
163
-
To calculate `x*pow(x, n-1)`, we need to make a subcall of `pow` with new arguments `pow(2, 2)`.
163
+
To calculate `x * pow(x, n - 1)`, we need to make a subcall of `pow` with new arguments `pow(2, 2)`.
164
164
165
165
### pow(2, 2)
166
166
@@ -230,7 +230,7 @@ function pow(x, n) {
230
230
231
231
There are no more nested calls, so the function finishes, returning `2`.
232
232
233
-
As the function finishes, its execution context is not needed any more, so it's removed from the memory. The previous one is restored off the top of the stack:
233
+
As the function finishes, its execution context is not needed anymore, so it's removed from the memory. The previous one is restored off the top of the stack:
234
234
235
235
236
236
<ul class="function-execution-context-list">
@@ -244,7 +244,7 @@ As the function finishes, its execution context is not needed any more, so it's
244
244
</li>
245
245
</ul>
246
246
247
-
The execution of `pow(2, 2)` is resumed. It has the result of the subcall `pow(2, 1)`, so it also can finish the evaluation of `x * pow(x, n-1)`, returning `4`.
247
+
The execution of `pow(2, 2)` is resumed. It has the result of the subcall `pow(2, 1)`, so it also can finish the evaluation of `x * pow(x, n - 1)`, returning `4`.
248
248
249
249
Then the previous context is restored:
250
250
@@ -269,7 +269,7 @@ A loop-based algorithm is more memory-saving:
269
269
function pow(x, n) {
270
270
let result = 1;
271
271
272
-
for(let i = 0; i < n; i++) {
272
+
for(let i = 0; i < n; i++) {
273
273
result *= x;
274
274
}
275
275
@@ -360,7 +360,7 @@ function sumSalaries(department) {
360
360
return department.reduce((prev, current) => prev + current.salary, 0); // sum the array
361
361
} else { // case (2)
362
362
let sum = 0;
363
-
for(let subdep of Object.values(department)) {
363
+
for(let subdep of Object.values(department)) {
364
364
sum += sumSalaries(subdep); // recursively call for subdepartments, sum the results
365
365
}
366
366
return sum;
@@ -395,7 +395,7 @@ A company *department* is:
395
395
- Either an array of people.
396
396
- Or an object with *departments*.
397
397
398
-
For web-developers there are much betterknown examples: HTML and XML documents.
398
+
For web-developers there are much better-known examples: HTML and XML documents.
399
399
400
400
In the HTML document, an *HTML-tag* may contain a list of:
0 commit comments