Skip to content
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer is `2`, that's the first truthy value.
Відповідь `2`, це перше правдиве значення.

```js run
alert( null || 2 || undefined );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What's the result of OR?
# Який результат АБО?

What is the code below going to output?
Що виведе код нижче?

```js
alert( null || 2 || undefined );
Expand Down
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The answer: first `1`, then `2`.
Відповідь: спочатку `1`, потім `2`.

```js run
alert( alert(1) || 2 || alert(3) );
```

The call to `alert` does not return a value. Or, in other words, it returns `undefined`.
Виклик `alert` не повертає значення. Або, іншими словами, повертає `undefined`.

1. The first OR `||` evaluates it's left operand `alert(1)`. That shows the first message with `1`.
2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value.
3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert.
1. Перший АБО `||` обчислює його лівий операнд `alert(1)`. Це показує перше повідомлення з `1`.
2. `alert` повертає `undefined`, тому АБО переходить до другого операнда, шукаючи правдиве значення.
3. Другий операнд `2` є правдивим, тому виконання зупинено, повернуто `2` і потім показано зовнішнім alert.

There will be no `3`, because the evaluation does not reach `alert(3)`.
Не буде `3`, тому що обчислення на досягає `alert(3)`.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What's the result of OR'ed alerts?
# Який результат alerts, об'єднаних АБО?

What will the code below output?
Що виведе код нижче?

```js
alert( alert(1) || 2 || alert(3) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `null`, because it's the first falsy value from the list.
Відповідь: `null`, тому що це перше не правдиве значення зі списку.

```js run
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What is the result of AND?
# Який результат І?

What is this code going to show?
Що виведе код нижче?

```js
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The answer: `1`, and then `undefined`.
Відповідь: `1`, а потім `undefined`.

```js run
alert( alert(1) && alert(2) );
```

The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return).
Виклик `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.
Через це `&&` обчислює лівий операнд (виводить `1`) і негайно зупиняється, оскільки `undefined` є не правдивим значенням. І `&&` шукає не правдиве значення і повертає його, як це і зроблено.

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/4-alert-and/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What is the result of AND'ed alerts?
# Який результат alerts, об'єднаних І?

What will this code show?
Що виведе код нижче?

```js
alert( alert(1) && alert(2) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
The answer: `3`.
Відповідь: `3`.

```js run
alert( null || 2 && 3 || 4 );
```

The precedence of AND `&&` is higher than `||`, so it executes first.
Пріоритет І `&&` вище за `||`, тому він виконується першим.

The result of `2 && 3 = 3`, so the expression becomes:
Результат `2 && 3 = 3`, тому вираз стає:

```
null || 3 || 4
```

Now the result is the first truthy value: `3`.
Тепер результат — перше правдиве значення: `3`.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The result of OR AND OR
# Результат АБО І АБО

What will the result be?
Який буде результат?

```js
alert( null || 2 && 3 || 4 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range between
# Перевірте діапазон

Write an "if" condition to check that `age` is between `14` and `90` inclusively.
Напишіть умову "if", щоб перевірити, що `age` знаходиться між `14` та `90` включно.

"Inclusively" means that `age` can reach the edges `14` or `90`.
"Включно" означає, що `age` може досягати країв `14` або `90`.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The first variant:
Перший варіант:

```js
if (!(age >= 14 && age <= 90))
```

The second variant:
Другий варіант:

```js
if (age < 14 || age > 90)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range outside
# Перевірте зовнішній діапазон

Write an `if` condition to check that `age` is NOT between 14 and 90 inclusively.
Напишіть умову `if`: щоб перевірити, що вік `age` НЕ знаходиться між 14 та 90 включно.

Create two variants: the first one using NOT `!`, the second one -- without it.
Створіть два варіанти: перший використовує НЕ `!`, другий -- без нього.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/11-logical-operators/8-if-question/solution.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
The answer: the first and the third will execute.
Відповідь: перший і третій виконаються.

Details:
Деталі:

```js run
// Runs.
// The result of -1 || 0 = -1, truthy
if (-1 || 0) alert( 'first' );
// Виконається.
// Результат -1 || 0 = -1, правдивий
if (-1 || 0) alert( 'перший' );

// Doesn't run
// -1 && 0 = 0, falsy
if (-1 && 0) alert( 'second' );
// Не виконається
// -1 && 0 = 0, не правдивий
if (-1 && 0) alert( 'другий' );

// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// Виконається
// Оператор && має більший приорітет, ніж ||
// тому -1 && 1 виконається першим, даючи нам послідовність:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
if (null || -1 && 1) alert( 'третій' );
```

12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/8-if-question/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# A question about "if"
# Питання про "if"

Which of these `alert`s are going to execute?
Який з цих `alert` буде виконано?

What will the results of the expressions be inside `if(...)`?
Які рузельтати виразів будуть у `if(...)`?

```js
if (-1 || 0) alert( 'first' );
if (-1 && 0) alert( 'second' );
if (null || -1 && 1) alert( 'third' );
if (-1 || 0) alert( 'перший' );
if (-1 && 0) alert( 'другий' );
if (null || -1 && 1) alert( 'третій' );
```

Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@


```js run demo
let userName = prompt("Who's there?", '');
let userName = prompt("Хто там?", '');

if (userName == 'Admin') {

let pass = prompt('Password?', '');
let pass = prompt('Пароль?', '');

if (pass == 'TheMaster') {
alert( 'Welcome!' );
alert( 'Ласкаво просимо!' );
} else if (pass == '' || pass == null) {
alert( 'Canceled.' );
alert( 'Скасовано.' );
} else {
alert( 'Wrong password' );
alert( 'Неправильний пароль' );
}

} else if (userName == '' || userName == null) {
alert( 'Canceled' );
alert( 'Скасовано' );
} else {
alert( "I don't know you" );
alert( "Я вас не знаю" );
}
```

Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
Зверніть увагу на вертикальні відступи у блоках `if`. Вони технічно не потрібні, але роблять код читабельним.
20 changes: 10 additions & 10 deletions 1-js/02-first-steps/11-logical-operators/9-check-login/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ importance: 3

---

# Check the login
# Перевірте логін

Write the code which asks for a login with `prompt`.
Напишіть код, який запитує логін за допомогою `prompt`.

If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or `key:Esc` -- show "Canceled.", if it's another string -- then show "I don't know you".
Якщо відвідувач вводить `"Admin"`, тоді запропонуйте за допомогою `prompt` ввести пароль, i якщо вхідні данні є порожнім рядком або `key:Esc` -- показати "Скасовано.", якщо це інакший рядок -- тоді покажіть "Я вас не знаю".

The password is checked as follows:
Пароль перевіряється наступним чином:

- If it equals "TheMaster", then show "Welcome!",
- Another string -- show "Wrong password",
- For an empty string or cancelled input, show "Canceled."
- Якщо він дорівнює "TheMaster", тоді покажіть "Ласкаво просимо!",
- Інший рядок -- покажіть "Неправильний пароль",
- Для порожнього рядка або введення було скасовано, покажіть "Скасовано."

The schema:
Схема:

![](ifelse_task.png)

Please use nested `if` blocks. Mind the overall readability of the code.
Будь ласка, використовуйте вкладені `if` блоки. Майте на увазі загальну читабельність коду.

Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`.
Підказка: передача порожнього вводу до запиту повертає порожній рядок `''`. Натискання `key:ESC` протягом запиту повертає `null`.

[demo]
Loading