logical tasks

This commit is contained in:
Ilya Kantor 2015-08-26 21:19:18 +03:00
parent 6a9d74609a
commit 337a15594a
8 changed files with 20 additions and 18 deletions

View file

@ -1,4 +1,4 @@
Ответ: `2`, это первое значение, которое в логическом контексте даст `true`.
The answer is `2`, that's the first truthy value.
```js
//+ run

View file

@ -1,8 +1,8 @@
# Что выведет alert (ИЛИ)?
# What's the result of OR?
[importance 5]
Что выведет код ниже?
What the code below is going to output?
```js
alert( null || 2 || undefined );

View file

@ -1,15 +1,16 @@
Ответ: сначала `1`, затем `2`.
The answer: first `1`, then `2`.
```js
//+ run
alert( alert(1) || 2 || alert(3) );
```
Вызов `alert` не возвращает значения, или, иначе говоря, возвращает `undefined`.
The call to `alert` does not return a value. Or, in other words, it returns `undefined`.
<ol>
<li>Первый оператор ИЛИ `||` выполнит первый `alert(1)`, получит `undefined` и пойдёт дальше, ко второму операнду.</li>
<li>Так как второй операнд `2` является истинным, то вычисления завершатся, результатом `undefined || 2` будет `2`, которое будет выведено внешним `alert( .... )`.</li>
<li>The first OR `||` evaluates it's left operand `alert(1)`. That shows the first message with `1`.</li>
<li>The `alert` returns `undefined`, so OR goes on to the second operand in it's search of a truthy value.</li>
<li>The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert.</li>
</ol>
Второй оператор `||` не будет выполнен, выполнение до `alert(3)` не дойдёт, поэтому `3` выведено не будет.
There will be no `3`, because the evaluation does not reach `alert(3)`.

View file

@ -1,8 +1,8 @@
# Что выведет alert (ИЛИ)?
# What's the result of OR'ed alerts?
[importance 3]
Что выведет код ниже?
What the code below will output?
```js
alert( alert(1) || 2 || alert(3) );

View file

@ -1,4 +1,4 @@
Ответ: `null`, это первое ложное значение из списка.
The answer: `null`, because it's the first falsy value from the list.
```js
//+ run

View file

@ -1,8 +1,8 @@
# Что выведет alert (И)?
# What is the result of AND?
[importance 5]
Что выведет код ниже?
What this code is going to show?
```js
alert( 1 && null && 2 );

View file

@ -1,10 +1,11 @@
Ответ: `1`, а затем `undefined`.
The answer: `1`, and then `undefined`.
```js
//+ run
alert( alert(1) && alert(2) );
```
The call to `alert` does not return a value or, in other words, returns `undefined`.
Вызов `alert` не возвращает значения, или, иначе говоря, возвращает `undefined`.
Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done.
Поэтому до правого `alert` дело не дойдёт, вычисления закончатся на левом.

View file

@ -1,8 +1,8 @@
# Что выведет alert (И)?
# What is the result of AND'ed alerts?
[importance 3]
Что выведет код ниже?
What will this code show?
```js
alert( alert(1) && alert(2) );