Skip to content

Commit 776dbc8

Browse files
committed
advanced functions main sync
1 parent 4fd7c97 commit 776dbc8

File tree

10 files changed

+18
-16
lines changed

10 files changed

+18
-16
lines changed

1-js/06-advanced-functions/01-recursion/01-sum-to/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
3737

3838
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
3939

40-
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory, so counting `sumTo(100000)` becomes possible. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
40+
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function, with no other calculations performed, then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ When `pow(x, n)` is called, the execution splits into two branches:
6161
if n==1 = x
6262
/
6363
pow(x, n) =
64-
\
64+
\
6565
else = x * pow(x, n - 1)
6666
```
6767

@@ -285,7 +285,7 @@ The iterative `pow` uses a single context changing `i` and `result` in the proce
285285
286286
**Any recursion can be rewritten as a loop. The loop variant usually can be made more effective.**
287287
288-
...But sometimes the rewrite is non-trivial, especially when function uses different recursive subcalls depending on conditions and merges their results or when the branching is more intricate. And the optimization may be unneeded and totally not worth the efforts.
288+
...But sometimes the rewrite is non-trivial, especially when a function uses different recursive subcalls depending on conditions and merges their results or when the branching is more intricate. And the optimization may be unneeded and totally not worth the efforts.
289289
290290
Recursion can give a shorter code, easier to understand and support. Optimizations are not required in every place, mostly we need a good code, that's why it's used.
291291
@@ -535,7 +535,7 @@ Terms:
535535
list = { value, next -> list }
536536
```
537537
538-
Trees like HTML elements tree or the department tree from this chapter are also naturally recursive: they branch and every branch can have other branches.
538+
Trees like HTML elements tree or the department tree from this chapter are also naturally recursive: they have branches and every branch can have other branches.
539539
540540
Recursive functions can be used to walk them as we've seen in the `sumSalary` example.
541541

1-js/06-advanced-functions/01-recursion/recursive-salaries.svg

Lines changed: 1 addition & 1 deletion
Loading

1-js/06-advanced-functions/02-rest-parameters-spread/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function sum(a, b) {
2323
alert( sum(1, 2, 3, 4, 5) );
2424
```
2525

26-
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
26+
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted, so the result in the code above is `3`.
2727

2828
The rest of the parameters can be included in the function definition by using three dots `...` followed by the name of the array that will contain them. The dots literally mean "gather the remaining parameters into an array".
2929

1-js/06-advanced-functions/03-closure/5-function-in-if/task.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
importance: 5
12

3+
---
24
# Function in if
35

46
Look at the code. What will be the result of the call at the last line?

1-js/06-advanced-functions/04-var/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ alert(test); // ReferenceError: test is not defined
5858

5959
The same thing for loops: `var` cannot be block- or loop-local:
6060

61-
```js
61+
```js run
6262
for (var i = 0; i < 10; i++) {
6363
var one = 1;
6464
// ...
@@ -170,7 +170,7 @@ That's best demonstrated with an example:
170170

171171
```js run
172172
function sayHi() {
173-
alert(phrase);
173+
alert(phrase);
174174

175175
*!*
176176
var phrase = "Hello";

1-js/06-advanced-functions/05-global-object/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var gVar = 5;
2525
alert(window.gVar); // 5 (became a property of the global object)
2626
```
2727

28-
The same effect have function declarations (statements with `function` keyword in the main code flow, not function expressions).
28+
Function declarations have the same effect (statements with `function` keyword in the main code flow, not function expressions).
2929

3030
Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such a thing doesn't happen.
3131

1-js/06-advanced-functions/08-settimeout-setinterval/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Usually, that's a function. For historical reasons, a string of code can be pass
2727
: The delay before run, in milliseconds (1000 ms = 1 second), by default 0.
2828

2929
`arg1`, `arg2`...
30-
: Arguments for the function (not supported in IE9-)
30+
: Arguments for the function
3131

3232
For instance, this code calls `sayHi()` after one second:
3333

@@ -102,7 +102,7 @@ As we can see from `alert` output, in a browser the timer identifier is a number
102102

103103
Again, there is no universal specification for these methods, so that's fine.
104104

105-
For browsers, timers are described in the [timers section](https://www.w3.org/TR/html5/webappapis.html#timers) of HTML5 standard.
105+
For browsers, timers are described in the [timers section](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) of HTML Living Standard.
106106

107107
## setInterval
108108

@@ -232,7 +232,7 @@ setTimeout(function() {...}, 100);
232232
233233
For `setInterval` the function stays in memory until `clearInterval` is called.
234234
235-
There's a side-effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
235+
There's a side effect. A function references the outer lexical environment, so, while it lives, outer variables live too. They may take much more memory than the function itself. So when we don't need the scheduled function anymore, it's better to cancel it, even if it's very small.
236236
````
237237

238238
## Zero delay setTimeout
@@ -256,7 +256,7 @@ The first line "puts the call into calendar after 0ms". But the scheduler will o
256256
There are also advanced browser-related use cases of zero-delay timeout, that we'll discuss in the chapter <info:event-loop>.
257257

258258
````smart header="Zero delay is in fact not zero (in a browser)"
259-
In the browser, there's a limitation of how often nested timers can run. The [HTML5 standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.".
259+
In the browser, there's a limitation of how often nested timers can run. The [HTML Living Standard](https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers) says: "after five nested timers, the interval is forced to be at least 4 milliseconds.".
260260
261261
Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself with zero delay. Each call remembers the real time from the previous one in the `times` array. What do the real delays look like? Let's see:
262262
@@ -297,6 +297,6 @@ Please note that all scheduling methods do not *guarantee* the exact delay.
297297
For example, the in-browser timer may slow down for a lot of reasons:
298298
- The CPU is overloaded.
299299
- The browser tab is in the background mode.
300-
- The laptop is on battery.
300+
- The laptop is on battery saving mode.
301301

302302
All that may increase the minimal timer resolution (the minimal delay) to 300ms or even 1000ms depending on the browser and OS-level performance settings.

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Create a "throttling" decorator `throttle(f, ms)` -- that returns a wrapper.
88

99
When it's called multiple times, it passes the call to `f` at maximum once per `ms` milliseconds.
1010

11-
The difference with debounce is that it's completely different decorator:
11+
Compared to the debounce decorator, the behavior is completely different:
1212
- `debounce` runs the function once after the "cooldown" period. Good for processing the final result.
1313
- `throttle` runs it not more often than given `ms` time. Good for regular updates that shouldn't be very often.
1414

1-js/06-advanced-functions/10-bind/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ for (let key in user) {
202202
}
203203
```
204204

205-
JavaScript libraries also provide functions for convenient mass binding , e.g. [_.bindAll(object, methodNames)](http://lodash.com/docs#bindAll) in lodash.
205+
JavaScript libraries also provide functions for convenient mass binding , e.g. [_.bindAll(object, methodNames)](https://lodash.com/docs#bindAll) in lodash.
206206
````
207207
208208
## Partial functions

0 commit comments

Comments
 (0)