Skip to content

Commit a851ef6

Browse files
authored
Update article.md
1 parent ccc0e93 commit a851ef6

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

1-js/06-advanced-functions/01-recursion/article.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ There are two ways to implement it.
3131
let result = 1;
3232

3333
// 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++) {
3535
result *= x;
3636
}
3737

@@ -67,8 +67,8 @@ pow(x, n) =
6767
else = x * pow(x, n - 1)
6868
```
6969

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`.
7272

7373
We can also say that `pow` *recursively calls itself* till `n == 1`.
7474

@@ -91,7 +91,7 @@ Here we can rewrite the same using the ternary `?` operator instead of `if` to m
9191

9292
```js run
9393
function pow(x, n) {
94-
return (n == 1) ? x : (x * pow(x, n-1));
94+
return (n == 1) ? x : (x * pow(x, n - 1));
9595
}
9696
```
9797
````
@@ -134,7 +134,7 @@ We can sketch it as:
134134
</li>
135135
</ul>
136136
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`:
138138
139139
```js run
140140
function pow(x, n) {
@@ -160,7 +160,7 @@ The variables are same, but the line changes, so the context is now:
160160
</li>
161161
</ul>
162162
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)`.
164164
165165
### pow(2, 2)
166166
@@ -230,7 +230,7 @@ function pow(x, n) {
230230
231231
There are no more nested calls, so the function finishes, returning `2`.
232232
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:
234234
235235
236236
<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
244244
</li>
245245
</ul>
246246
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`.
248248
249249
Then the previous context is restored:
250250
@@ -269,7 +269,7 @@ A loop-based algorithm is more memory-saving:
269269
function pow(x, n) {
270270
let result = 1;
271271
272-
for(let i = 0; i < n; i++) {
272+
for (let i = 0; i < n; i++) {
273273
result *= x;
274274
}
275275
@@ -360,7 +360,7 @@ function sumSalaries(department) {
360360
return department.reduce((prev, current) => prev + current.salary, 0); // sum the array
361361
} else { // case (2)
362362
let sum = 0;
363-
for(let subdep of Object.values(department)) {
363+
for (let subdep of Object.values(department)) {
364364
sum += sumSalaries(subdep); // recursively call for subdepartments, sum the results
365365
}
366366
return sum;
@@ -395,7 +395,7 @@ A company *department* is:
395395
- Either an array of people.
396396
- Or an object with *departments*.
397397
398-
For web-developers there are much better known examples: HTML and XML documents.
398+
For web-developers there are much better-known examples: HTML and XML documents.
399399
400400
In the HTML document, an *HTML-tag* may contain a list of:
401401
- Text pieces.

0 commit comments

Comments
 (0)