From 40a48662f1caf455364ba49df95fda8d867a31ca Mon Sep 17 00:00:00 2001 From: Otmar Onderek Date: Mon, 18 Apr 2022 22:35:48 +0200 Subject: [PATCH 01/10] 1.2.11 --- .../1-alert-null-2-undefined/solution.md | 2 +- .../1-alert-null-2-undefined/task.md | 4 +- .../2-alert-or/solution.md | 12 +- .../11-logical-operators/2-alert-or/task.md | 4 +- .../3-alert-1-null-2/solution.md | 2 +- .../3-alert-1-null-2/task.md | 4 +- .../4-alert-and/solution.md | 7 +- .../11-logical-operators/4-alert-and/task.md | 4 +- .../5-alert-and-or/solution.md | 8 +- .../5-alert-and-or/task.md | 4 +- .../6-check-if-in-range/solution.md | 2 +- .../6-check-if-in-range/task.md | 6 +- .../7-check-if-out-range/solution.md | 8 +- .../7-check-if-out-range/task.md | 6 +- .../8-if-question/solution.md | 24 +- .../8-if-question/task.md | 12 +- .../9-check-login/ifelse_task.svg | 2 +- .../9-check-login/solution.md | 22 +- .../9-check-login/task.md | 20 +- .../11-logical-operators/article.md | 224 +++++++++--------- 20 files changed, 187 insertions(+), 190 deletions(-) diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md index 8869d32e6..5b7759b2f 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md @@ -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 ); diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md index a7c9addfc..d2992822c 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md @@ -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 ); diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md index f85b56366..a81b9f5db 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md @@ -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. diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md index 3908fa2ec..96cee19ad 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md @@ -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) ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md index 5c2455ef4..6f4096ad2 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md @@ -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 ); diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md index 043d431e4..aa3bdc892 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md @@ -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 ); diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md index b6fb10d72..ecf3c2696 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md @@ -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. diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md index 69f877b95..8608f0cc2 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md @@ -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) ); diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md index 25e3568f8..d5407434f 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md @@ -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`. diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md index b18bb9c51..6e0298d58 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md @@ -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 ); diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md index 87c733b22..7dbe60181 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md @@ -1,6 +1,6 @@ ```js -if (age >= 14 && age <= 90) +if (věk >= 14 && věk <= 90) ``` diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md index fc9e336c1..f2fc19e9c 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md @@ -2,8 +2,6 @@ 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. - -"Inclusively" means that `age` can reach the edges `14` or `90`. +Napište podmínku „if“, která ověří, zda proměnná `věk` má hodnotu mezi `14` a `90` včetně. „Včetně“ znamená, že podmínka je splněna i tehdy, je-li `věk` `14` nebo `90`. diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md index d1946a967..c67031b6e 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md @@ -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) ``` diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md index 9b947d00f..6c3bdb7ea 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md @@ -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. diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md index 210509758..87044d2cc 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md @@ -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í' ); ``` diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md index 55987121b..a488c57fa 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md @@ -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í' ); ``` diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg b/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg index d22b518a9..9e4caf654 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/ifelse_task.svg @@ -1 +1 @@ -BeginCanceledCanceledWelcome!I don't know youWrong passwordWho's there?Password?CancelCancelAdminTheMasterOtherOther \ No newline at end of file +ZačátekZrušenoZrušenoVítejte!Neznám vásChybné hesloKdo je tam?Heslo?ZrušenoZrušenoAdminMistrJinéJiné \ No newline at end of file diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index 604606259..2096d4837 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -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 pass = prompt('Heslo?', ''); - if (pass === 'TheMaster') { - alert( 'Welcome!' ); + if (pass === 'Mistr') { + alert( 'Vítáme vás!' ); } else if (pass === '' || pass === null) { - alert( 'Canceled' ); + 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. diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index 290a52642..839e7e0a4 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -2,24 +2,24 @@ importance: 3 --- -# Check the login +# Ověřte login -Write the code which asks for a login with `prompt`. +Napište kód, který se pomocí `prompt` zeptá na login. -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". +Jestliže návštěvník zadá `"Správce"`, zeptejte se ho pomocí `prompt` na heslo. Bude-li vstupem prázdný řádek nebo uživatel stiskne `key:Esc`, zobrazte „Zrušeno“. Bude-li zadán jiný řetězec, zobrazte „Neznám vás“. -The password is checked as follows: +Heslo se ověří následovně: -- If it equals "TheMaster", then show "Welcome!", -- Another string -- show "Wrong password", -- For an empty string or cancelled input, show "Canceled" +- Pokud se rovná „Mistr“, pak zobrazte „Vítáme vás!“. +- Při jiném řetězci zobrazte „Špatné heslo“. +- Bude-li zadán prázdný řetězec nebo bude vstup zrušen, zobrazte „Zrušeno“. -The schema: +Schéma: ![](ifelse_task.svg) -Please use nested `if` blocks. Mind the overall readability of the code. +Použijte vnořené bloky `if`. Dbejte na celkovou čitelnost kódu. -Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`. +Rada: po zadání prázdného vstupu `prompt` vrátí prázdný řetězec `''`. Po stisknutí klávesy `key:ESC` vrátí `null`. [demo] diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 78c4fd2f1..e357c4393 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,24 +1,24 @@ -# Logical operators +# Logické operátory -There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), `??` (Nullish Coalescing). Here we cover the first three, the `??` operator is in the next article. +V JavaScriptu jsou čtyři logické operátory: `||` (OR -- nebo), `&&` (AND -- a), `!` (NOT -- ne), `??` (koalescence). V tomto článku vysvětlíme první tři, operátor `??` bude vysvětlen v dalším článku. -Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. +Ačkoli se nazývají „logické“, mohou být použity na hodnoty libovolného typu, nejenom boolean. Také jejich výsledek může být jakéhokoli typu. -Let's see the details. +Podívejme se na podrobnosti. ## || (OR) -The "OR" operator is represented with two vertical line symbols: +Operátor „OR“ („nebo“) je reprezentován dvěma svislými čarami za sebou: ```js -result = a || b; +výsledek = a || b; ``` -In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`. +V klasickém programování je logické OR určeno pouze k manipulaci s booleovskými hodnotami. Je-li některý z jeho argumentů `true`, vrátí `true`, jinak vrátí `false`. -In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values. +V JavaScriptu je tento operátor trochu rafinovanější a silnější. Nejprve se však podívejme, co se stane s hodnotami typu boolean. -There are four possible logical combinations: +Existují čtyři možné logické kombinace: ```js run alert( true || true ); // true @@ -27,124 +27,124 @@ alert( true || false ); // true alert( false || false ); // false ``` -As we can see, the result is always `true` except for the case when both operands are `false`. +Jak vidíme, výsledek je vždy `true` kromě případu, kdy jsou oba operandy `false`. -If an operand is not a boolean, it's converted to a boolean for the evaluation. +Není-li operand typu boolean, je pro účel vyhodnocení převeden na boolean. -For instance, the number `1` is treated as `true`, the number `0` as `false`: +Například číslo `1` se převede na `true`, číslo `0` na `false`: ```js run -if (1 || 0) { // works just like if( true || false ) - alert( 'truthy!' ); +if (1 || 0) { // funguje stejně jako if ( true || false ) + alert( 'pravda!' ); } ``` -Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`. +Většinou se OR `||` používá v příkazu `if` k otestování, zda *některá* ze zadaných podmínek je `true`. -For example: +Příklad: ```js run -let hour = 9; +let hodina = 9; *!* -if (hour < 10 || hour > 18) { +if (hodina < 10 || hodina > 18) { */!* - alert( 'The office is closed.' ); + alert( 'Úřad má zavřeno.' ); } ``` -We can pass more conditions: +Můžeme předat další podmínky: ```js run -let hour = 12; -let isWeekend = true; +let hodina = 12; +let jeVíkend = true; -if (hour < 10 || hour > 18 || isWeekend) { - alert( 'The office is closed.' ); // it is the weekend +if (hodina < 10 || hodina > 18 || jeVíkend) { + alert( 'Úřad má zavřeno.' ); // je víkend } ``` -## OR "||" finds the first truthy value [#or-finds-the-first-truthy-value] +## OR „||“ najde první pravdivou hodnotu [#or-finds-the-first-truthy-value] -The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript. +Výše uvedená logika je vcelku klasická. Nyní se podívejme na „extra“ vlastnosti JavaScriptu. -The extended algorithm works as follows. +Rozšířený algoritmus funguje následovně. -Given multiple OR'ed values: +Zadáme více hodnot, spojených ORem: ```js -result = value1 || value2 || value3; +výsledek = hodnota1 || hodnota2 || hodnota3; ``` -The OR `||` operator does the following: +Operátor OR `||` provádí následující: -- Evaluates operands from left to right. -- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were `false`), returns the last operand. +- Vyhodnocuje operandy zleva doprava. +- Každý operand převede na typ boolean. Je-li výsledek `true`, zastaví se a vrátí původní hodnotu tohoto operandu. +- Pokud byly vyhodnoceny všechny operandy (tj. všechny byly `false`), vrátí poslední operand. -A value is returned in its original form, without the conversion. +Hodnota se vrátí ve své původní podobě bez konverze. -In other words, a chain of OR `||` returns the first truthy value or the last one if no truthy value is found. +Jinými slovy, řetězec ORů `"||"` vrátí první pravdivou hodnotu, a pokud není žádná pravdivá hodnota nalezena, vrátí poslední hodnotu. -For instance: +Příklad: ```js run -alert( 1 || 0 ); // 1 (1 is truthy) +alert( 1 || 0 ); // 1 (1 je pravdivá) -alert( null || 1 ); // 1 (1 is the first truthy value) -alert( null || 0 || 1 ); // 1 (the first truthy value) +alert( null || 1 ); // 1 (1 je první pravdivá hodnota) +alert( null || 0 || 1 ); // 1 (první pravdivá hodnota) -alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) +alert( undefined || null || 0 ); // 0 (všechny jsou nepravdivé, vrátí poslední hodnotu) ``` -This leads to some interesting usage compared to a "pure, classical, boolean-only OR". +To umožňuje některé zajímavé způsoby použití ve srovnání s „čistým, klasickým, pouze booleovským ORem“. -1. **Getting the first truthy value from a list of variables or expressions.** +1. **Získání první pravdivé hodnoty ze seznamu proměnných nebo výrazů.** - For instance, we have `firstName`, `lastName` and `nickName` variables, all optional (i.e. can be undefined or have falsy values). - - Let's use OR `||` to choose the one that has the data and show it (or `"Anonymous"` if nothing set): + Například máme proměnné `jméno`, `příjmení` a `přezdívka`, všechny jsou nepovinné (tj. mohou být `undefined` nebo obsahovat hodnoty nepravda). + + Vybereme pomocí OR `||` tu, která obsahuje data, a zobrazíme je (není-li nastaveno nic, zobrazíme `anonym`): ```js run - let firstName = ""; - let lastName = ""; - let nickName = "SuperCoder"; + let jméno = ""; + let příjmení = ""; + let přezdívka = "SuperCoder"; *!* - alert( firstName || lastName || nickName || "Anonymous"); // SuperCoder + alert( jméno || příjmení || přezdívka || "anonym"); // SuperCoder */!* ``` - If all variables were falsy, `"Anonymous"` would show up. + Kdyby všechny proměnné obsahovaly hodnotu nepravda, zobrazil by se `anonym`. -2. **Short-circuit evaluation.** +2. **Zkrácené vyhodnocení.** - Another feature of OR `||` operator is the so-called "short-circuit" evaluation. + Další vlastností operátoru OR `||` je tzv. „zkrácené“ vyhodnocení. - It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument. + Znamená to, že `||` zpracovává své argumenty, dokud nenarazí na první pravdivou hodnotu, a pak tuto hodnotu okamžitě vrátí, aniž by se byť jen dotkl dalších argumentů. - The importance of this feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call. + Důležitost této vlastnosti se projeví, jestliže operandem nebude pouhá hodnota, ale výraz s vedlejším efektem, například přiřazení proměnné nebo volání funkce. - In the example below, only the second message is printed: + V níže uvedeném příkladu se zobrazí jen druhá zpráva: ```js run no-beautify - *!*true*/!* || alert("not printed"); - *!*false*/!* || alert("printed"); + *!*true*/!* || alert("nezobrazí se"); + *!*false*/!* || alert("zobrazí se"); ``` - In the first line, the OR `||` operator stops the evaluation immediately upon seeing `true`, so the `alert` isn't run. + Operátor OR `||` na prvním řádku se přestane vyhodnocovat ihned poté, co narazí na `true`, takže `alert` se nevykoná. - Sometimes, people use this feature to execute commands only if the condition on the left part is falsy. + Někdy lidé využívají tuto vlastnost k tomu, aby vykonali příkaz jen tehdy, není-li splněna podmínka na levé straně. ## && (AND) -The AND operator is represented with two ampersands `&&`: +Operátor „AND“ („a“) je reprezentován dvěma znaky „ampersand“ `&&`: ```js -result = a && b; +výsledek = a && b; ``` -In classical programming, AND returns `true` if both operands are truthy and `false` otherwise: +V klasickém programování AND vrací `true`, jsou-li oba operandy pravdivé, a `false` jinak: ```js run alert( true && true ); // true @@ -153,137 +153,137 @@ alert( true && false ); // false alert( false && false ); // false ``` -An example with `if`: +Příklad s `if`: ```js run -let hour = 12; -let minute = 30; +let hodina = 12; +let minuta = 30; -if (hour == 12 && minute == 30) { - alert( 'The time is 12:30' ); +if (hodina == 12 && minuta == 30) { + alert( 'Je právě 12:30' ); } ``` -Just as with OR, any value is allowed as an operand of AND: +Stejně jako u OR může být operandem AND jakákoli hodnota: ```js run -if (1 && 0) { // evaluated as true && false - alert( "won't work, because the result is falsy" ); +if (1 && 0) { // vyhodnotí se jako true && false + alert( "nevykoná se, protože výsledkem je nepravda" ); } ``` -## AND "&&" finds the first falsy value +## AND „&&“ najde první nepravdivou hodnotu -Given multiple AND'ed values: +Mějme několik hodnot spojených ANDem: ```js -result = value1 && value2 && value3; +výsledek = hodnota1 && hodnota2 && hodnota3; ``` -The AND `&&` operator does the following: +Operátor AND `&&` provádí následující: -- Evaluates operands from left to right. -- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were truthy), returns the last operand. +- Vyhodnocuje operandy zleva doprava. +- Každý operand převede na typ boolean. Je-li výsledek `false`, zastaví se a vrátí původní hodnotu tohoto operandu. +- Pokud byly vyhodnoceny všechny operandy (tj. všechny byly pravdivé), vrátí poslední operand. -In other words, AND returns the first falsy value or the last value if none were found. +Jinými slovy, řetězec ANDů `"||"` vrátí první nepravdivou hodnotu, a pokud není žádná nepravdivá hodnota nalezena, vrátí poslední hodnotu. -The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one. +Výše uvedená pravidla se podobají operátoru OR. Rozdíl spočívá v tom, že AND najde první *nepravdivou* hodnotu, zatímco OR najde první *pravdivou*. -Examples: +Příklady: ```js run -// if the first operand is truthy, -// AND returns the second operand: +// je-li první operand pravdivý, +// AND vrátí druhý operand: alert( 1 && 0 ); // 0 alert( 1 && 5 ); // 5 -// if the first operand is falsy, -// AND returns it. The second operand is ignored +// je-li první operand nepravdivý, +// AND ho vrátí. Druhý operand se pak ignoruje. alert( null && 5 ); // null -alert( 0 && "no matter what" ); // 0 +alert( 0 && "je jedno co" ); // 0 ``` -We can also pass several values in a row. See how the first falsy one is returned: +Můžeme předat i více hodnot za sebou. Podívejme se, jak se vrátí první nepravdivá: ```js run alert( 1 && 2 && null && 3 ); // null ``` -When all values are truthy, the last value is returned: +Když jsou všechny hodnoty pravdivé, vrátí se poslední z nich: ```js run -alert( 1 && 2 && 3 ); // 3, the last one +alert( 1 && 2 && 3 ); // 3, poslední hodnota ``` -````smart header="Precedence of AND `&&` is higher than OR `||`" -The precedence of AND `&&` operator is higher than OR `||`. +````smart header="Priorita AND `&&` je vyšší než priorita OR `||`" +Priorita operátoru AND `&&` je vyšší než priorita operátoru OR `||`. -So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`. +Kód `a && b || c && d` je tedy v zásadě stejný, jako kdyby výrazy s `&&` byly uzavřeny do závorek: `(a && b) || (c && d)`. ```` -````warn header="Don't replace `if` with `||` or `&&`" -Sometimes, people use the AND `&&` operator as a "shorter way to write `if`". +````warn header="Nenahrazujte příkaz `if` operátorem || nebo &&" +Někdy lidé používají operátor AND `&&` jako „kratší variantu `if`“. -For instance: +Příklad: ```js run let x = 1; -(x > 0) && alert( 'Greater than zero!' ); +(x > 0) && alert( 'Větší než nula!' ); ``` -The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true. +Akce napravo od `&&` se vykoná jen tehdy, když k ní dospěje vyhodnocování, tedy jen když `(x > 0)` platí. -So we basically have an analogue for: +Máme tedy v zásadě analogii k: ```js run let x = 1; -if (x > 0) alert( 'Greater than zero!' ); +if (x > 0) alert( 'Větší než nula!' ); ``` -Although, the variant with `&&` appears shorter, `if` is more obvious and tends to be a little bit more readable. So we recommend using every construct for its purpose: use `if` if we want `if` and use `&&` if we want AND. +Ačkoli varianta s `&&` se zdá být kratší, `if` je zřejmější a obvykle bývá trochu čitelnější. Doporučujeme tedy používat každou konstrukci k tomu, k čemu byla stvořena: používejte `if`, chcete-li „pokud“, a používejte `&&`, chcete-li AND. ```` ## ! (NOT) -The boolean NOT operator is represented with an exclamation sign `!`. +Booleovský operátor „NOT“ („ne“) je reprezentován vykřičníkem `!`. -The syntax is pretty simple: +Jeho syntaxe je velice jednoduchá: ```js -result = !value; +výsledek = !hodnota; ``` -The operator accepts a single argument and does the following: +Tento operátor přijímá jediný argument a provádí následující: -1. Converts the operand to boolean type: `true/false`. -2. Returns the inverse value. +1. Převede operand na typ boolean: `true/false`. +2. Vrátí opačnou hodnotu. -For instance: +Příklad: ```js run alert( !true ); // false alert( !0 ); // true ``` -A double NOT `!!` is sometimes used for converting a value to boolean type: +Někdy se používá dvojité NOT `!!` ke konverzi hodnoty na typ boolean: ```js run -alert( !!"non-empty string" ); // true +alert( !!"neprázdný řetězec" ); // true alert( !!null ); // false ``` -That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. +Znamená to, že první NOT převede hodnotu na boolean a vrátí opak, druhý NOT jej znovu převrátí. Na konci tedy máme planou konverzi na boolean. -There's a little more verbose way to do the same thing -- a built-in `Boolean` function: +Existuje trochu výmluvnější způsob, jak udělat totéž -- vestavěná funkce `Boolean`: ```js run -alert( Boolean("non-empty string") ); // true +alert( Boolean("neprázdný řetězec") ); // true alert( Boolean(null) ); // false ``` -The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`. +Operátor NOT `!` má nejvyšší prioritu ze všech logických operátorů, takže se vždy vykoná jako první, dříve než `&&` nebo `||`. From 3fad7eca58b268351ef43a44400b90015a35d12d Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:44:20 +0200 Subject: [PATCH 02/10] Update 1-js/02-first-steps/11-logical-operators/article.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- 1-js/02-first-steps/11-logical-operators/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index e357c4393..88b5becb3 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -187,7 +187,7 @@ Operátor AND `&&` provádí následující: - Každý operand převede na typ boolean. Je-li výsledek `false`, zastaví se a vrátí původní hodnotu tohoto operandu. - Pokud byly vyhodnoceny všechny operandy (tj. všechny byly pravdivé), vrátí poslední operand. -Jinými slovy, řetězec ANDů `"||"` vrátí první nepravdivou hodnotu, a pokud není žádná nepravdivá hodnota nalezena, vrátí poslední hodnotu. +Jinými slovy, řetězec ANDů `"&&"` vrátí první nepravdivou hodnotu, a pokud není žádná nepravdivá hodnota nalezena, vrátí poslední hodnotu. Výše uvedená pravidla se podobají operátoru OR. Rozdíl spočívá v tom, že AND najde první *nepravdivou* hodnotu, zatímco OR najde první *pravdivou*. From 02780ea1ff63b2bd795776af3b83733f39d8aeb8 Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:08 +0200 Subject: [PATCH 03/10] Update 1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- .../11-logical-operators/6-check-if-in-range/task.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md index f2fc19e9c..2ac202d14 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md @@ -4,4 +4,6 @@ importance: 3 # Ověřte, zda je hodnota v rozsahu -Napište podmínku „if“, která ověří, zda proměnná `věk` má hodnotu mezi `14` a `90` včetně. „Včetně“ znamená, že podmínka je splněna i tehdy, je-li `věk` `14` nebo `90`. +Napište podmínku „if“, která ověří, že proměnná `věk` má hodnotu mezi `14` a `90` včetně. + +„Včetně“ znamená, že podmínka je splněna i tehdy, je-li `věk` `14` nebo `90`. From a60e98fe790806aed49858f575a8761d5c74bab6 Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:18 +0200 Subject: [PATCH 04/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/task.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- 1-js/02-first-steps/11-logical-operators/9-check-login/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index 839e7e0a4..d5af76d4e 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -2,7 +2,7 @@ importance: 3 --- -# Ověřte login +# Ověřte přihlášení Napište kód, který se pomocí `prompt` zeptá na login. From b0623744cad4e36122e6e3b43caff0718966ae59 Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:24 +0200 Subject: [PATCH 05/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/task.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- 1-js/02-first-steps/11-logical-operators/9-check-login/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index d5af76d4e..e0614f4b3 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -4,7 +4,7 @@ importance: 3 # Ověřte přihlášení -Napište kód, který se pomocí `prompt` zeptá na login. +Napište kód, který se pomocí `prompt` zeptá na přihlášení. Jestliže návštěvník zadá `"Správce"`, zeptejte se ho pomocí `prompt` na heslo. Bude-li vstupem prázdný řádek nebo uživatel stiskne `key:Esc`, zobrazte „Zrušeno“. Bude-li zadán jiný řetězec, zobrazte „Neznám vás“. From ab291f2a97d454a36e131340f5f7942f98470bde Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:39 +0200 Subject: [PATCH 06/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/task.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- 1-js/02-first-steps/11-logical-operators/9-check-login/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index e0614f4b3..d5954c83c 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -10,7 +10,7 @@ Jestliže návštěvník zadá `"Správce"`, zeptejte se ho pomocí `prompt` na Heslo se ověří následovně: -- Pokud se rovná „Mistr“, pak zobrazte „Vítáme vás!“. +- Pokud se rovná „Vládce“, tak zobrazte „Vítáme vás!“. - Při jiném řetězci zobrazte „Špatné heslo“. - Bude-li zadán prázdný řetězec nebo bude vstup zrušen, zobrazte „Zrušeno“. From 78f1df58fea6a555adf0e0e2b11781775709056d Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:46 +0200 Subject: [PATCH 07/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/task.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- 1-js/02-first-steps/11-logical-operators/9-check-login/task.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index d5954c83c..d4fad802a 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -20,6 +20,6 @@ Schéma: Použijte vnořené bloky `if`. Dbejte na celkovou čitelnost kódu. -Rada: po zadání prázdného vstupu `prompt` vrátí prázdný řetězec `''`. Po stisknutí klávesy `key:ESC` vrátí `null`. +Nápověda: po zadání prázdného vstupu `prompt` vrátí prázdný řetězec `''`. Po stisknutí klávesy `key:ESC` vrátí `null`. [demo] From 474789eab15c7225f9f312736145cf0f3d2bc26e Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:52 +0200 Subject: [PATCH 08/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/solution.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- .../11-logical-operators/9-check-login/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index 2096d4837..6cebefe28 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -5,7 +5,7 @@ let uživatelskéJméno = prompt("Kdo je tam?", ''); if (uživatelskéJméno === 'Správce') { - let pass = prompt('Heslo?', ''); + let heslo = prompt('Heslo?', ''); if (pass === 'Mistr') { alert( 'Vítáme vás!' ); From 017bd688153cbad2bc89c696a4401fed4a770896 Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:46:58 +0200 Subject: [PATCH 09/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/solution.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- .../11-logical-operators/9-check-login/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index 6cebefe28..1b27bf739 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -7,7 +7,7 @@ if (uživatelskéJméno === 'Správce') { let heslo = prompt('Heslo?', ''); - if (pass === 'Mistr') { + if (heslo === 'Vládce') { alert( 'Vítáme vás!' ); } else if (pass === '' || pass === null) { alert( 'Zrušeno' ); From 970cbed396cfb3e2e285232891c35adf051d6592 Mon Sep 17 00:00:00 2001 From: otmon76 <66325539+otmon76@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:47:05 +0200 Subject: [PATCH 10/10] Update 1-js/02-first-steps/11-logical-operators/9-check-login/solution.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Pomajbík <60559058+danipoma@users.noreply.github.com> --- .../11-logical-operators/9-check-login/solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index 1b27bf739..bcdd7d6f6 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -9,7 +9,7 @@ if (uživatelskéJméno === 'Správce') { if (heslo === 'Vládce') { alert( 'Vítáme vás!' ); - } else if (pass === '' || pass === null) { + } else if (heslo === '' || heslo === null) { alert( 'Zrušeno' ); } else { alert( 'Špatné heslo' );