Skip to content

Logical operators #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer is `2`, that's the first truthy value.
Odpověď zní `2`, což je první pravdivá hodnota.

```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?
# Jaký je výsledek OR?

What is the code below going to output?
Co vypíše níže uvedený kód?

```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`.
Odpověď zní: nejprve `1`, pak `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`.
Volání `alert` nevrátí žádnou hodnotu, jinými slovy vrátí `undefined`.

1. The first OR `||` evaluates its 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. První OR `||` vyhodnotí svůj levý operand `alert(1)`. Ten zobrazí první zprávu obsahující `1`.
2. Tento `alert` vrátí `undefined`, takže OR ve svém hledání pravdivé hodnoty přejde ke druhému operandu.
3. Druhý operand `2` je pravdivý, takže vyhodnocení operátoru se zastaví, vrátí `2` a to je pak zobrazeno vnějším `alert`em.

There will be no `3`, because the evaluation does not reach `alert(3)`.
Nezobrazí se `3`, protože vyhodnocení se k `alert(3)` nedostane.
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?
# Jaký je výsledek alertů spojených ORem?

What will the code below output?
Co vypíše níže uvedený kód?

```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.
Odpověď zní `null`, protože je to první nepravdivá hodnota v seznamu.

```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?
# Jaký je výsledek AND?

What is this code going to show?
Co vypíše níže uvedený kód?

```js
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
The answer: `1`, and then `undefined`.
Odpověď zní: nejprve `1`, pak `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).

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.
Volání `alert` vrátí `undefined` (funkce jen zobrazí zprávu, takže její návratová hodnota nemá žádný význam).

Proto `&&` vyhodnotí první operand (vypíše `1`) a okamžitě se zastaví, protože `undefined` je nepravdivá hodnota. Operátor `&&` hledá první nepravdivou hodnotu a vrátí ji, takže je hotov.
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?
# Jaký je výsledek alertů spojených ANDem?

What will this code show?
Co vypíše níže uvedený kód?

```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`.
Odpověď zní: `3`.

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

The precedence of AND `&&` is higher than `||`, so it executes first.
Operátor AND `&&` má vyšší prioritu než `||`, proto se vykoná jako první.

The result of `2 && 3 = 3`, so the expression becomes:
Výsledek `2 && 3 = 3`, takže z výrazu se stane:

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

Now the result is the first truthy value: `3`.
Nyní bude výsledkem první pravdivá hodnota: `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
# Výsledek OR AND OR

What will the result be?
Jaký bude výsledek?

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


```js
if (age >= 14 && age <= 90)
if (věk >= 14 && věk <= 90)
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range between
# Ověřte, zda je hodnota v rozsahu

Write an `if` condition to check that `age` is between `14` and `90` inclusively.
Napište podmínku „if“, která ověří, že proměnná `věk` má hodnotu mezi `14` a `90` včetně.

"Inclusively" means that `age` can reach the edges `14` or `90`.
„Včetně“ znamená, že podmínka je splněna i tehdy, je-li `věk` `14` nebo `90`.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
The first variant:
První varianta:

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

The second variant:
Druhá varianta:

```js
if (age < 14 || age > 90)
if (věk < 14 || věk > 90)
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range outside
# Ověřte, zda hodnota je mimo rozsah

Write an `if` condition to check that `age` is NOT between `14` and `90` inclusively.
Napište podmínku „if“, která ověří, zda proměnná `věk` NENÍ mezi `14` a `90` včetně.

Create two variants: the first one using NOT `!`, the second one -- without it.
Vytvořte dvě varianty: první bude používat NOT `!`, druhá se obejde bez něj.
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.
Odpověď zní: vykoná se první a třetí `alert`.

Details:
Podrobnosti:

```js run
// Runs.
// The result of -1 || 0 = -1, truthy
if (-1 || 0) alert( 'first' );
// Vykoná se.
// Výsledek -1 || 0 = -1, tedy pravda.
if (-1 || 0) alert( 'první' );

// Doesn't run
// -1 && 0 = 0, falsy
if (-1 && 0) alert( 'second' );
// Nevykoná se.
// -1 && 0 = 0, nepravda.
if (-1 && 0) alert( 'druhý' );

// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// Vykoná se.
// Operátor && má vyšší prioritu než ||,
// takže -1 && 1 se vykoná jako první, což dává tento řetězec:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
if (null || -1 && 1) alert( 'třetí' );
```

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"
# Otázka na „if“

Which of these `alert`s are going to execute?
Který z těchto `alert`ů se vykoná?

What will the results of the expressions be inside `if(...)`?
Jaké budou výsledky výrazů uvnitř `if(...)`?

```js
if (-1 || 0) alert( 'first' );
if (-1 && 0) alert( 'second' );
if (null || -1 && 1) alert( 'third' );
if (-1 || 0) alert( 'první' );
if (-1 && 0) alert( 'druhý' );
if (null || -1 && 1) alert( 'třetí' );
```

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/11-logical-operators/9-check-login/solution.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@


```js run demo
let userName = prompt("Who's there?", '');
let uživatelskéJméno = prompt("Kdo je tam?", '');

if (userName === 'Admin') {
if (uživatelskéJméno === 'Správce') {

let pass = prompt('Password?', '');
let heslo = prompt('Heslo?', '');

if (pass === 'TheMaster') {
alert( 'Welcome!' );
} else if (pass === '' || pass === null) {
alert( 'Canceled' );
if (heslo === 'Vládce') {
alert( 'Vítáme vás!' );
} else if (heslo === '' || heslo === null) {
alert( 'Zrušeno' );
} else {
alert( 'Wrong password' );
alert( 'Špatné heslo' );
}

} else if (userName === '' || userName === null) {
alert( 'Canceled' );
} else if (uživatelskéJméno === '' || uživatelskéJméno === null) {
alert( 'Zrušeno' );
} else {
alert( "I don't know you" );
alert( "Neznám vás" );
}
```

Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
Všimněte si svislého odsazení uvnitř bloků `if`. Není technicky vyžadováno, ale činí kód čitelnějším.
Loading