Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Loops: while and for#79

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
danipoma merged 23 commits intojavascript-tutorial:masterfromotmon76:1.2.13
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
22e67d2
1.2.13
otmon76Apr 18, 2022
ac45f55
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
604a3a3
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
d1cfebf
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
5f77e5d
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
6b7ec81
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
8cfd1f4
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
5d83267
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
91b1ce1
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
9dae4f5
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
3f5b411
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
365b270
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
e4315be
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
657a600
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
e6c02a3
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
be6052f
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
4e96419
Update 1-js/02-first-steps/13-while-for/article.md
otmon76Jun 23, 2022
7d3a5f3
Update 1-js/02-first-steps/13-while-for/2-which-value-while/solution.md
otmon76Jun 23, 2022
7e040fa
Update 1-js/02-first-steps/13-while-for/2-which-value-while/solution.md
otmon76Jun 23, 2022
010b94b
Update 1-js/02-first-steps/13-while-for/2-which-value-while/solution.md
otmon76Jun 23, 2022
5d9d5ff
Update 1-js/02-first-steps/13-while-for/2-which-value-while/task.md
otmon76Jun 23, 2022
f8fe05a
Update 1-js/02-first-steps/13-while-for/2-which-value-while/task.md
otmon76Jun 23, 2022
fb0c0cf
Merge branch 'master' into 1.2.13
danipomaJun 23, 2022
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
The answer: `1`.
Odpověď: `1`.

```js run
let i = 3;
Expand All@@ -8,18 +8,18 @@ while (i) {
}
```

Every loop iteration decreases `i`by `1`.The check`while(i)`stops the loop when `i = 0`.
Každá iterace cyklu sníží `i`o `1`.Ověření`while(i)`zastaví cyklus, když `i = 0`.

Hence, the steps of the loop form the following sequence ("loop unrolled"):
Jednotlivé kroky cyklu tedy vytvoří následující posloupnost:

```js
let i = 3;

alert(i--); //shows 3,decreases ito 2
alert(i--); //zobrazí 3,sníží ina 2

alert(i--) //shows 2,decreases ito 1
alert(i--) //zobrazí 2,sníží ina 1

alert(i--) //shows 1,decreases ito 0
alert(i--) //zobrazí 1,sníží ina 0

//done,while(i)check stops the loop
//hotovo, ověřeníwhile(i)ukončí cyklus
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,9 +2,9 @@ importance: 3

---

#Last loop value
#Poslední hodnota v cyklu

What is the last value alerted by this code? Why?
Jaká je poslední hodnota, kterou vypíše tento kód? Proč?

```js
let i = 3;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
Tato úloha ukazuje, jak může prefixová a postfixová notace vést k rozdílným výsledkům, když je použijeme v porovnání.

1. **From 1to 4**
1. **Od 1do 4**

```js run
let i = 0;
while (++i < 5) alert( i );
```

The first value is`i = 1`,because `++i`first increments `i`and then returns the new value. So the first comparison is`1 < 5`and the`alert`shows `1`.
První hodnota je`i = 1`,jelikož `++i`nejprve zvýší `i`a pak vrátí novou hodnotu. První porovnání je tedy`1 < 5`a`alert`zobrazí `1`.

Then follow`2, 3, 4…` --the values show up one after another. The comparison always uses the incremented value, because `++`is before the variable.
Následují`2, 3, 4…` --hodnoty se zobrazí jedna po druhé. Porovnání se vždy dívá na zvýšenou hodnotu, protože `++`je před proměnnou.

Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown.
2. **From 1 to 5**
Nakonec `i = 4` se zvýší na `5`, porovnání `while(5 < 5)` neuspěje a cyklus skončí. Takže `5` se nezobrazí.

2. **Od 1 do 5**

```js run
let i = 0;
while (i++ < 5) alert( i );
```

The first value is again`i = 1`.The postfix form of`i++`increments `i`and then returns the *old* value, so the comparison`i++ < 5`will use`i = 0` (contrary to `++i < 5`).
První hodnota je opět`i = 1`.Postfixová notace`i++`zvýší `i`a pak vrátí *starou* hodnotu, takže porovnání`i++ < 5`se dívá na`i = 0` (na rozdíl od `++i < 5`).

But the`alert`call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`.
Avšak`alert`se volá odděleně. Je to další příkaz, který se spustí až po zvýšení a porovnání. Proto obdrží aktuální `i = 1`.

Then follow `2, 3, 4…`
Následují `2, 3, 4…`

Let's stop on `i = 4`.The prefix form`++i`would increment it and use `5` in the comparison. But here we have the postfix form`i++`.So it increments`i`to `5`,but returns the old value. Hence the comparison is actually`while(4 < 5)` --true, and the control goes on to `alert`.
Zastavme se u `i = 4`.Prefixová notace`++i`by je zvýšila a v porovnání by použila `5`. Tady však máme postfixovou notaci`i++`.Ta zvýší`i`na `5`,ale vrátí starou hodnotu. Proto se provede porovnání`while(4 < 5)` --pravda, tudíž řízení přejde k `alert`.

The value`i = 5`is the last one, because on the next step`while(5 < 5)`is false.
Hodnota`i = 5`je poslední, jelikož další krok`while(5 < 5)`dává nepravdu.
11 changes: 5 additions & 6 deletions1-js/02-first-steps/13-while-for/2-which-value-while/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,19 +2,18 @@ importance: 4

---

#Which values does the while loop show?
#Které hodnoty zobrazí cyklus while?

For every loop iteration, write down which value it outputs and then compare it with the solution.
Pro oba cykly si zapište hodnoty, které budou vypsány, a pak je porovnejte s řešením.

Both loops `alert` the same values, or not?

1. The prefix form `++i`:
Vypíše `alert` v obou cyklech stejné hodnoty, nebo ne?

1. Prefixová notace `++i`:
```js
let i = 0;
while (++i < 5) alert( i );
```
2.The postfix form `i++`
2.Postfixová notace `i++`

```js
let i = 0;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
**The answer: from `0`to `4`in both cases.**
**Odpověď: od `0`do `4`v obou případech.**

```js run
for (let i = 0; i < 5; ++i) alert( i );

for (let i = 0; i < 5; i++) alert( i );
```

That can be easily deducted from the algorithm of `for`:
Lze to snadno odvodit z algoritmu pro `for`:

1.Execute once`i = 0`before everything (begin).
2.Check the condition `i < 5`
3.If `true` -- execute the loop body`alert(i)`, and then `i++`
1.Nejdříve se jedenkrát vykoná`i = 0`(začátek).
2.Ověří se podmínka `i < 5`.
3.Je-li `true`, vykoná se tělo cyklu`alert(i)` a pak `i++`.

The increment`i++`is separated from the condition check(2).That's just another statement.
Zvýšení`i++`je odděleno od testu podmínky(2).Je to jen další příkaz.

The value returned by the increment is not used here, so there's no difference between`i++`and `++i`.
Hodnota vrácená zvýšením se tady nepoužívá, takže mezi`i++`a `++i` zde není žádný rozdíl.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Hodnota vrácená zvýšením se tady nepoužívá, takže mezi`i++` a`++i`zdenení žádný rozdíl.
Hodnota vrácená zvýšením se tady nepoužívá, takže mezi`i++` a`++i` není žádný rozdíl.

10 changes: 5 additions & 5 deletions1-js/02-first-steps/13-while-for/3-which-value-for/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,18 +2,18 @@ importance: 4

---

#Which values get shown by the "for" loop?
#Které hodnoty zobrazí cyklus „for?

For each loop write down which values it is going to show. Then compare with the answer.
Pro oba cykly zapište hodnoty, které budou zobrazeny, a pak je porovnejte s řešením.

Both loops`alert`same values or not?
Vypíše`alert`v obou cyklech stejné hodnoty, nebo ne?

1.The postfix form:
1.Postfixová forma:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
1. Postfixováforma:
1. Postfixovánotace:


```js
for (let i = 0; i < 5; i++) alert( i );
```
2.The prefix form:
2.Prefixová forma:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
2. Prefixováforma:
2. Prefixovánotace:


```js
for (let i = 0; i < 5; ++i) alert( i );
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,4 +8,4 @@ for (let i = 2; i <= 10; i++) {
}
```

We use the "modulo" operator `%` to get the remainder and check for the evenness here.
K získání zbytku po dělení dvěma a ověření sudosti zde používáme operátor „modulo“ `%`.
4 changes: 2 additions & 2 deletions1-js/02-first-steps/13-while-for/4-for-even/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,8 @@ importance: 5

---

#Output even numbers in the loop
#Vypište sudá čísla v cyklu

Use the `for`loop to output even numbers from`2`to `10`.
Použitím cyklu `for`vypište sudá čísla od`2`do `10`.

[demo]
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@
```js run
let i = 0;
while (i < 3) {
alert( `number ${i}!` );
alert( `číslo ${i}!` );
i++;
}
```
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,13 +2,13 @@ importance: 5

---

#Replace "for" with "while"
#Nahraďte cyklus „for“ cyklem „while

Rewrite the code changing the `for`loop to`while` without altering its behavior (the output should stay same).
Přepište kód tak, že cyklus `for`zaměníte za cyklus`while`, aniž by se změnilo jeho chování (výstup má zůstat stejný).

```js run
for (let i = 0; i < 3; i++) {
alert( `number ${i}!` );
alert( `číslo ${i}!` );
}
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@

```js run demo
letnum;
letčíslo;

do {
num = prompt("Enter a number greater than100?", 0);
} while (num <= 100 &&num);
číslo = prompt("Zadejte číslo větší než100", 0);
} while (číslo <= 100 &&číslo);
```

The loop`do..while`repeats while both checks are truthy:
Cyklus`do..while`se opakuje, dokud jsou obě podmínky splněny:

1.The check for `num <= 100` --that is, the entered value is still not greater than `100`.
2.The check`&&num` is false when `num` is `null`or an empty string. Then the`while`loop stops too.
1.Podmínka `číslo <= 100` --tedy že zadaná hodnota stále není větší než `100`.
2.Podmínka`&&číslo` je nepravdivá, když `číslo` je `null`nebo prázdný řetězec. Pak se cyklus`while`rovněž zastaví.

P.S.If `num` is `null` then `num <= 100`is `true`,so without the 2nd check the loop wouldn't stop if the user clicks CANCEL. Both checks are required.
P.S.Jestliže `číslo` je `null`, pak `číslo <= 100`vydá `true`,takže bez druhé podmínky by se cyklus nezastavil, kdyby uživatel stiskl Storno. Obě podmínky jsou zapotřebí.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,12 +2,12 @@ importance: 5

---

#Repeat until the input is correct
#Opakujte, dokud vstup nebude správně

Write a loop which prompts for a number greater than `100`.If the visitor enters another number -- ask them to input again.
Napište cyklus, který se bude ptát na číslo větší než `100`.Pokud návštěvník zadá jiné číslo, zeptejte se ho znovu.

The loop must ask for a number until either the visitor enters a number greater than`100`or cancels the input/enters an empty line.
Cyklus se musí ptát na číslo tak dlouho, až návštěvník zadá číslo větší než`100`nebo zruší vstup či zadá prázdný řádek.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Cyklus se musí ptát na číslo tak dlouho, návštěvník zadá číslo větší než`100` nebo zruší vstup či zadá prázdný řádek.
Cyklus se musí ptát na číslo tak dlouho,než návštěvník zadá číslo větší než`100` nebo zruší vstup či zadá prázdný řádek.


Here we can assume that the visitor only inputs numbers. There's no need to implement a special handling for a non-numeric input in this task.
Zde můžeme předpokládat, že uživatel zadává pouze čísla. V této úloze nemusíte implementovat zvláštní zacházení s nečíselnými vstupy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Zdemůžeme předpokládat, že uživatel zadává pouze čísla. V této úloze nemusíte implementovat zvláštní zacházení s nečíselnými vstupy.
Zdepředpokládáme, že uživatel zadává pouze čísla. V této úloze nemusíte implementovat zvláštní zacházení s nečíselnými vstupy.


[demo]
26 changes: 13 additions & 13 deletions1-js/02-first-steps/13-while-for/7-list-primes/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
There are many algorithms for this task.
Pro tuto úlohu existuje mnoho algoritmů.

Let's use a nested loop:
Použijeme vnořený cyklus:

```js
For each iin the interval {
check if ihas a divisor from 1..i
if yes =>the value is not a prime
if no =>the value is a prime, show it
pro každé iv intervalu {
ověř, zda imá dělitele mezi 1..i
pokud ano =>i není prvočíslo
pokud ne =>i je prvočíslo, zobraz ho
}
```

The code using a label:
Kód s použitím návěští:

```js run
let n = 10;

nextPrime:
for (let i = 2; i <= n; i++) { //for each i...
dalšíPrvočíslo:
for (let i = 2; i <= n; i++) { //pro každé i...

for (let j = 2; j < i; j++) { //look for a divisor..
if (i % j == 0) continuenextPrime; //not a prime, go next i
for (let j = 2; j < i; j++) { //hledáme dělitele...
if (i % j == 0) continuedalšíPrvočíslo; //není to prvočíslo, přejdeme k dalšímu i
}

alert( i ); //a prime
alert( i ); //je to prvočíslo
}
```

There's a lot of space to optimize it. For instance, we could look for the divisors from`2`to square root of`i`.But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve)etc.
Je zde mnoho prostoru k optimalizaci. Můžeme se například dívat jen na dělitele od`2`do odmocniny`i`.Kdybychom však chtěli být opravdu efektivní i pro velké intervaly, museli bychom změnit přístup a zaměřit se na vysokou matematiku a složité algoritmy, např. [kvadratické síto](https://en.wikipedia.org/wiki/Quadratic_sieve), [Obecné číselné teoretické síto (GNFS)](https://en.wikipedia.org/wiki/General_number_field_sieve)atd.
14 changes: 7 additions & 7 deletions1-js/02-first-steps/13-while-for/7-list-primes/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,16 +2,16 @@ importance: 3

---

#Output prime numbers
#Vypište prvočísla

An integer number greater than`1`is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except`1`and itself.
Celé číslo větší než`1`se nazývá [prvočíslo](https://cs.wikipedia.org/wiki/Prvočíslo), jestliže není dělitelné beze zbytku jiným celým číslem než`1`a sebou samým.

In other words,`n > 1`is a prime if it can't be evenly divided by anything except`1`and `n`.
Jinými slovy, číslo`n > 1`je prvočíslo, jestliže není dělitelné beze zbytku jiným číslem než`1`a `n`.

For example,`5`is a prime, because it cannot be divided without a remainder by`2`, `3`and `4`.
Například`5`je prvočíslo, protože není dělitelné beze zbytku číslem`2`, `3`ani `4`.

**Write the code which outputs prime numbers in the interval from`2`to `n`.**
**Napište kód, který vypíše všechna prvočísla v intervalu od`2`do `n`.**

For`n = 10`the result will be `2,3,5,7`.
Například pro`n = 10`bude výsledek `2,3,5,7`.

P.S.The code should work for any `n`, not be hard-tuned for any fixed value.
P.S.Kód by měl fungovat pro jakékoli `n`. Neměl by být vyladěn jen pro nějakou pevnou hodnotu.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp