Skip to content

Basic operators, maths #74

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 17 commits into from
May 17, 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
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/08-operators/1-increment-order/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The answer is:
Odpověď zní:

- `a = 2`
- `b = 2`
Expand All @@ -9,10 +9,10 @@ The answer is:
```js run no-beautify
let a = 1, b = 1;

alert( ++a ); // 2, prefix form returns the new value
alert( b++ ); // 1, postfix form returns the old value
alert( ++a ); // 2, prefixová notace vrátí novou hodnotu
alert( b++ ); // 1, postfixová notace vrátí starou hodnotu

alert( a ); // 2, incremented once
alert( b ); // 2, incremented once
alert( a ); // 2, zvýšeno jedenkrát
alert( b ); // 2, zvýšeno jedenkrát
```

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/1-increment-order/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The postfix and prefix forms
# Postfixová a prefixová notace

What are the final values of all variables `a`, `b`, `c` and `d` after the code below?
Jaké jsou konečné hodnoty všech proměnných `a`, `b`, `c` a `d` po provedení uvedeného kódu?

```js
let a = 1, b = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The answer is:
Odpověď zní:

- `a = 4` (multiplied by 2)
- `x = 5` (calculated as 1 + 4)
- `a = 4` (vynásobí se 2)
- `x = 5` (vypočítá se jako 1 + 4)

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

---

# Assignment result
# Výsledek přiřazení

What are the values of `a` and `x` after the code below?
Jaké jsou hodnoty proměnných `a` a `x` po provedení uvedeného kódu?

```js
let a = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ undefined + 1 = NaN // (6)
" \t \n" - 2 = -2 // (7)
```

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 subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
3. The addition with a string appends the number `5` to the string.
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
5. `null` becomes `0` after the numeric conversion.
6. `undefined` becomes `NaN` after the numeric conversion.
7. Space characters, are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
1. Sčítání s řetězcem `"" + 1` převede `1` na řetězec: `"" + 1 = "1"`, pak tedy budeme mít `"1" + 0` a použije se stejné pravidlo.
2. Odčítání `-` (stejně jako většina matematických operací) pracuje jen s čísly, takže převede prázdný řetězec `""` na `0`.
3. Sčítání s řetězcem připojí k řetězci číslo `5`.
4. Odčítání vždy převádí operandy na čísla, takže vyrobí z `" -9 "` číslo `-9` (mezery okolo něj se ignorují).
5. `null` se převede na číslo `0`.
6. `undefined` se převede na číslo `NaN`.
7. Když se řetězec převádí na číslo, mezerové znaky se z jeho začátku a konce odříznou. V tomto případě se celý řetězec skládá z mezerových znaků, konkrétně `\t`, `\n` a „obyčejné“ mezery mezi nimi. Stejně jako prázdný řetězec se tedy převede na `0`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Type conversions
# Typová konverze

What are results of these expressions?
Jaké jsou výsledky následujících výrazů?

```js no-beautify
"" + 1 + 0
Expand All @@ -23,4 +23,4 @@ undefined + 1
" \t \n" - 2
```

Think well, write down and then compare with the answer.
Dobře si to promyslete, zapište si výsledky a pak je porovnejte s odpovědí.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
The reason is that prompt returns user input as a string.
Důvodem je, že funkce `prompt` vrací uživatelský vstup jako řetězec.

So variables have values `"1"` and `"2"` respectively.
V proměnných jsou tedy hodnoty po řadě `"1"` a `"2"`.

```js run
let a = "1"; // prompt("First number?", 1);
let b = "2"; // prompt("Second number?", 2);
let a = "1"; // prompt("První číslo?", 1);
let b = "2"; // prompt("Druhé číslo?", 2);

alert(a + b); // 12
```

What we should do is to convert strings to numbers before `+`. For example, using `Number()` or prepending them with `+`.
To, co bychom měli udělat, je před sečtením převést řetězce na čísla. Například použít `Number()` nebo před ně uvést `+`.

For example, right before `prompt`:
Například rovnou před `prompt`:

```js run
let a = +prompt("First number?", 1);
let b = +prompt("Second number?", 2);
let a = +prompt("První číslo?", 1);
let b = +prompt("Druhé číslo?", 2);

alert(a + b); // 3
```

Or in the `alert`:
Nebo až při volání `alert`:

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("První číslo?", 1);
let b = prompt("Druhé číslo?", 2);

alert(+a + +b); // 3
```

Using both unary and binary `+` in the latest code. Looks funny, doesn't it?
V posledním kódu používáme současně unární i binární `+`. Vypadá to legračně, že?
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Fix the addition
# Opravte sčítání

Here's a code that asks the user for two numbers and shows their sum.
Následující kód se zeptá uživatele na dvě čísla a zobrazí jejich součet.

It works incorrectly. The output in the example below is `12` (for default prompt values).
Nefunguje však správně. Výstup v níže uvedeném příkladu (pro defaultní hodnoty v dotazech) je `12`.

Why? Fix it. The result should be `3`.
Proč? Opravte jej. Výsledek by měl být `3`.

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("První číslo?", 1);
let b = prompt("Druhé číslo?", 2);

alert(a + b); // 12
```
Loading