This commit is contained in:
Ilya Kantor 2017-09-28 01:14:56 +03:00
commit 5b16f003cc
13 changed files with 33 additions and 32 deletions

View file

@ -17,6 +17,6 @@ undefined + 1 = NaN // (4)
``` ```
1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied. 1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
2. The substruction `"-"` (like most math operations) only works with numbers, it converts an empty string `""` to `0`. 2. The subtraction `"-"` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
3. `null` becomes `0` after the numeric conversion. 3. `null` becomes `0` after the numeric conversion.
4. `undefined` becomes `NaN` after the numeric conversion. 4. `undefined` becomes `NaN` after the numeric conversion.

View file

@ -173,7 +173,7 @@ alert( b ); // 4
alert( c ); // 4 alert( c ); // 4
``` ```
Chained assignments evaluate from right to left. First the rightmost expression `2+2` is evaluated then assigned to the variables on the left: `c`, `b` and `a`. At the end, all variables share a single value. Chained assignments evaluate from right to left. First the rightmost expression `2 + 2` is evaluated then assigned to the variables on the left: `c`, `b` and `a`. At the end, all variables share a single value.
````smart header="The assignment operator `\"=\"` returns a value" ````smart header="The assignment operator `\"=\"` returns a value"
An operator always returns a value. That's obvious for most of them like an addition `+` or a multiplication `*`. But the assignment operator follows that rule too. An operator always returns a value. That's obvious for most of them like an addition `+` or a multiplication `*`. But the assignment operator follows that rule too.
@ -381,8 +381,8 @@ This notation can be shortened using operators `+=` and `*=`:
```js run ```js run
let n = 2; let n = 2;
n += 5; // now n=7 (same as n = n + 5) n += 5; // now n = 7 (same as n = n + 5)
n *= 2; // now n=14 (same as n = n * 2) n *= 2; // now n = 14 (same as n = n * 2)
alert( n ); // 14 alert( n ); // 14
``` ```
@ -409,18 +409,18 @@ For example:
```js run ```js run
*!* *!*
let a = (1+2, 3+4); let a = (1 + 2, 3 + 4);
*/!* */!*
alert( a ); // 7 (the result of 3+4) alert( a ); // 7 (the result of 3 + 4)
``` ```
Here, the first expression `1+2` is evaluated, and its result is thrown away, then `3+4` is evaluated and returned as the result. Here, the first expression `1 + 2` is evaluated, and its result is thrown away, then `3 + 4` is evaluated and returned as the result.
```smart header="Comma has a very low precedence" ```smart header="Comma has a very low precedence"
Please note that the comma operator has very low precedence, lower than `=`, so parentheses are important in the example above. Please note that the comma operator has very low precedence, lower than `=`, so parentheses are important in the example above.
Without them: `a=1+2,3+4` evaluates `+` first, summing the numbers into `a=3,7`, then the assignment operator `=` assigns `a=3`, and then the number after the comma `7` is not processed anyhow, so it's ignored. Without them: `a = 1 + 2, 3 + 4` evaluates `+` first, summing the numbers into `a = 3, 7`, then the assignment operator `=` assigns `a = 3`, and then the number after the comma `7` is not processed anyhow, so it's ignored.
``` ```
Why do we need such an operator which throws away everything except the last part? Why do we need such an operator which throws away everything except the last part?

View file

@ -8,8 +8,8 @@ for (let i = 0; i < 5; i++) alert( i );
That can be easily deducted from the algorithm of `for`: That can be easily deducted from the algorithm of `for`:
1. Execute once `i=0` before everything (begin). 1. Execute once `i = 0` before everything (begin).
2. Check the condition `i<5` 2. Check the condition `i < 5`
3. If `true` -- execute the loop body `alert(i)`, and then `i++` 3. If `true` -- execute the loop body `alert(i)`, and then `i++`
The increment `i++` is separated from the condition check (2). That's just another statement. The increment `i++` is separated from the condition check (2). That's just another statement.

View file

@ -6,12 +6,12 @@ importance: 3
An integer number greater than `1` is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except `1` and itself. An integer number greater than `1` is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except `1` and itself.
In other words, `n>1` is a prime if it can't be evenly divided by anything except `1` and `n`. In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`.
For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`. For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`.
**Write the code which outputs prime numbers in the interval from `2` to `n`.** **Write the code which outputs prime numbers in the interval from `2` to `n`.**
For `n=10` the result will be `2,3,5,7`. For `n = 10` the result will be `2,3,5,7`.
P.S. The code should work for any `n`, not be hard-tuned for any fixed value. P.S. The code should work for any `n`, not be hard-tuned for any fixed value.

View file

@ -21,7 +21,7 @@ while (condition) {
While the `condition` is `true`, the `code` from the loop body is executed. While the `condition` is `true`, the `code` from the loop body is executed.
For instance, the loop below outputs `i` while `i<3`: For instance, the loop below outputs `i` while `i < 3`:
```js run ```js run
let i = 0; let i = 0;
@ -37,7 +37,7 @@ If there were no `i++` in the example above, the loop would repeat (in theory) f
Any expression or a variable can be a loop condition, not just a comparison. They are evaluated and converted to boolean by `while`. Any expression or a variable can be a loop condition, not just a comparison. They are evaluated and converted to boolean by `while`.
For instance, the shorter way to write `while (i!=0)` could be `while (i)`: For instance, the shorter way to write `while (i != 0)` could be `while (i)`:
```js run ```js run
let i = 3; let i = 3;
@ -108,8 +108,8 @@ Let's examine the `for` statement part by part:
| part | | | | part | | |
|-------|----------|----------------------------------------------------------------------------| |-------|----------|----------------------------------------------------------------------------|
| begin | `i=0` | Executes once upon entering the loop. | | begin | `i = 0` | Executes once upon entering the loop. |
| condition | `i<3`| Checked before every loop iteration, if fails the loop stops. | | condition | `i < 3`| Checked before every loop iteration, if fails the loop stops. |
| step| `i++` | Executes after the body on each iteration, but before the condition check. | | step| `i++` | Executes after the body on each iteration, but before the condition check. |
| body | `alert(i)`| Runs again and again while the condition is truthy | | body | `alert(i)`| Runs again and again while the condition is truthy |
@ -188,11 +188,11 @@ We can also remove the `step` part:
let i = 0; let i = 0;
for (; i < 3;) { for (; i < 3;) {
alert( i ); alert( i++ );
} }
``` ```
The loop became identical to `while (i<3)`. The loop became identical to `while (i < 3)`.
We can actually remove everything, thus creating an infinite loop: We can actually remove everything, thus creating an infinite loop:
@ -324,7 +324,7 @@ The ordinary `break` after `input` would only break the inner loop. That's not s
A *label* is an identifier with a colon before a loop: A *label* is an identifier with a colon before a loop:
```js ```js
labelName: for(...) { labelName: for (...) {
... ...
} }
``` ```
@ -369,7 +369,7 @@ For example, it is impossible to do this:
```js ```js
break label; // jumps to label? No. break label; // jumps to label? No.
label: for(...) label: for (...)
``` ```
The call to a `break/continue` is only possible from inside the loop, and the label must be somewhere upwards from the directive. The call to a `break/continue` is only possible from inside the loop, and the label must be somewhere upwards from the directive.
@ -381,7 +381,7 @@ We covered 3 types of loops:
- `while` -- The condition is checked before each iteration. - `while` -- The condition is checked before each iteration.
- `do..while` -- The condition is checked after each iteration. - `do..while` -- The condition is checked after each iteration.
- `for(;;)` -- The condition is checked before each iteration, additional settings available. - `for (;;)` -- The condition is checked before each iteration, additional settings available.
To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive. To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive.

View file

@ -403,9 +403,9 @@ If we have only one argument, then parentheses can be omitted, making that even
```js run ```js run
// same as // same as
// let double = function(n) { return n*2 } // let double = function(n) { return n * 2 }
*!* *!*
let double = n => n*2; let double = n => n * 2;
*/!* */!*
alert( double(3) ); // 6 alert( double(3) ); // 6

View file

@ -216,7 +216,7 @@ switch (age) {
alert("Won't work"); // the result of prompt is a string, not a number alert("Won't work"); // the result of prompt is a string, not a number
case "18": case "18":
alert("This works!""); alert("This works!");
break; break;
default: default:
@ -273,7 +273,7 @@ We covered three ways to create a function in JavaScript:
- Functions may have local variables: those declared inside its body. Such variables are only visible inside the function. - Functions may have local variables: those declared inside its body. Such variables are only visible inside the function.
- Parameters can have default values: `function sum(a=1, b=2) {...}`. - Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
- Functions always return something. If there's no `return` statement, then the result is `undefined`. - Functions always return something. If there's no `return` statement, then the result is `undefined`.

View file

@ -58,8 +58,9 @@ function showPrimes(n) {
function isPrime(n) { function isPrime(n) {
for (let i = 2; i < n; i++) { for (let i = 2; i < n; i++) {
if ( n % i == 0) return false; if (n % i == 0) return false;
} }
return true; return true;
} }
``` ```

View file

@ -9,7 +9,7 @@ Till now we've only seen strings. Now let's see the advantages that symbols can
## Symbols ## Symbols
"Symbol" value represents an unique identifier. "Symbol" value represents a unique identifier.
A value of this type can be created using `Symbol()`: A value of this type can be created using `Symbol()`:
@ -141,7 +141,7 @@ let user = {
}; };
*!* *!*
for(let key in user) alert(key); // name, age (no symbols) for (let key in user) alert(key); // name, age (no symbols)
*/!* */!*
// the direct access by the symbol works // the direct access by the symbol works

View file

@ -157,7 +157,7 @@ The first thing is nice. The second is tolerable, because we can use `Array.from
```warn header="DOM collections are read-only" ```warn header="DOM collections are read-only"
DOM collections, and even more -- *all* navigation properties listed in this chapter are read-only. DOM collections, and even more -- *all* navigation properties listed in this chapter are read-only.
We can't replace an child by something else assigning `childNodes[i] = ...`. We can't replace a child by something else assigning `childNodes[i] = ...`.
Changing DOM needs other methods, we'll see them in the next chapter. Changing DOM needs other methods, we'll see them in the next chapter.
``` ```

View file

@ -9,7 +9,7 @@ The caret `pattern:^` matches at the beginning of the text, and the dollar `patt
For instance, let's test if the text starts with `Mary`: For instance, let's test if the text starts with `Mary`:
```js run ```js run
let str1 = 'Mary had a little lamb, it's fleece was white as snow'; let str1 = "Mary had a little lamb, it's fleece was white as snow";
let str2 = 'Everywhere Mary went, the lamp was sure to go'; let str2 = 'Everywhere Mary went, the lamp was sure to go';
alert( /^Mary/.test(str1) ); // true alert( /^Mary/.test(str1) ); // true

View file

@ -121,7 +121,7 @@ What happen during the search of `pattern:(\d+)*$` in the line `subject:12345678
\d+....... \d+.......
(123456789)z (123456789)z
``` ```
2. Then it tries to apply the start around the parentheses `pattern:(\d+)*`, but there are no more digits, so it the star doesn't give anything. 2. Then it tries to apply the star around the parentheses `pattern:(\d+)*`, but there are no more digits, so it the star doesn't give anything.
Then the pattern has the string end anchor `pattern:$`, and in the text we have `subject:z`. Then the pattern has the string end anchor `pattern:$`, and in the text we have `subject:z`.

View file

@ -49,7 +49,7 @@ As the result is passed along the chain of handlers, we can see a sequence of `a
![](promise-then-chain.png) ![](promise-then-chain.png)
The whole thing works, because a call to `promise.then` returns a promise, so that we can call next `.then` on it. The whole thing works, because a call to `promise.then` returns a promise, so that we can call the next `.then` on it.
When a handler returns a value, it becomes the result of that promise, so the next `.then` is called with it. When a handler returns a value, it becomes the result of that promise, so the next `.then` is called with it.