Skip to content

Translated while for section #80

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 19 commits into from
Aug 16, 2021
14 changes: 7 additions & 7 deletions 1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `1`.
Atsakymas: `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`.
Kiekviena ciklo iteracija sumažina `i` per `1`. Patikrinimas `while(i)` sustabdo ciklą kai `i = 0`.

Hence, the steps of the loop form the following sequence ("loop unrolled"):
Dėl to žingsniai iš paskutinio ciklo suformuoja tokią seką ("atskleistas ciklas"):

```js
let i = 3;

alert(i--); // shows 3, decreases i to 2
alert(i--); // parodo 3, sumažina i iki 2

alert(i--) // shows 2, decreases i to 1
alert(i--) // parodo 2, sumažina i iki 1

alert(i--) // shows 1, decreases i to 0
alert(i--) // parodo 1, sumažina i iki 0

// done, while(i) check stops the loop
// viskas, while(i) patikrinimas sustabdo ciklą
```
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/12-while-for/1-loop-last-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Last loop value
# Paskutinė ciklo vertė

What is the last value alerted by this code? Why?
Kokia yra paskutinė šio kodo vertė parodyta per alert? Kodėl?

```js
let i = 3;
Expand Down
22 changes: 11 additions & 11 deletions 1-js/02-first-steps/12-while-for/2-which-value-while/solution.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
Užduotis parodo kaip priešdėlinė/podėlinė formos gali atvesti prie skirtingų rezultatų kai naudojamos palyginimui.

1. **From 1 to 4**
1. **Nuo 1 iki 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`.
Pirmoji vertė yra `i = 1`, nes `++i` pirma padidina `i` ir tada sugrąžina naują vertę. Tad pirmasis palyginimas yra `1 < 5` ir `alert` parodo `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.
Tada seka `2, 3, 4…` -- vertės pasirodo viena po kitos. Palyginimui visada naudojama jau padidinta vertė, nes `++` yra prieš kintamąjį.

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**
Galų gale, `i = 4` yra padidinamas iki `5`, tad palyginimas `while(5 < 5)` yra netiesa ir ciklas sustoja. Tad `5` nėra parodomas.
2. **Nuo 1 iki 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`).
Pirmoji vertė vėlgi yra `i = 1`. Podėlinė `i++` forma padidina `i` ir tada sugrąžina *senąją* vertę, tad palyginimas `i++ < 5` naudos `i = 0` (priešingai nei `++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`.
Bet šaukimas `alert` yra atskiras, Tai yra kitas teiginys, kuris yra įvykdomas po padidinimo ir palyginimo. Tad jis gauna esamą `i = 1`.

Then follow `2, 3, 4…`
Toliau seka `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`.
Sustokime prie `i = 4`. Priešdėlinė forma `++i` padidintų jį ir naudotų `5` palyginime. Bet čia mes turime podėlinę formą `i++`. Tad ji padidina `i` iki `5`, bet grąžina senąją vertę. Dėl to palyginimas yra iš tikrųjų `while(4 < 5)` -- tiesa, ir controlė pereina prie `alert`.

The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false.
Vertė `i = 5` yra paskutinė, nes sekančiame žingsnyje `while(5 < 5)` būtų netiesa.
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/12-while-for/2-which-value-while/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ importance: 4

---

# Which values does the while loop show?
# Kurias vertes parodo while ciklas?

For every loop iteration, write down which value it outputs and then compare it with the solution.
Kiekvienai ciklo iteracijai užrašykite kurią vertę ji atiduoda ir tada palygininkite tą vertę su sprendimu.

Both loops `alert` the same values, or not?
Abu ciklai `alert` tas pačias vertes ar ne?

1. The prefix form `++i`:
1. Priešdėlinė forma `++i`:

```js
let i = 0;
while (++i < 5) alert( i );
```
2. The postfix form `i++`
2. Podėlinė forma `i++`

```js
let i = 0;
Expand Down
14 changes: 7 additions & 7 deletions 1-js/02-first-steps/12-while-for/3-which-value-for/solution.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
**The answer: from `0` to `4` in both cases.**
**Atsakymas: nuo `0` iki `4` abiem atvejais.**

```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`:
Tai galima labai lengvai nustatyti iš `for` algoritmo:

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. Įvykdyti vieną kartą `i = 0` prieš visa kita (pradžia).
2. Patikrinti sąlygą `i < 5`
3. Jeigu `true` -- įvykdyti ciklo korpusą `alert(i)`, o tada `i++`

The increment `i++` is separated from the condition check (2). That's just another statement.
Padidėjimas `i++` yra atskirtas nuo sąlygos patikrinimo (2). Tai tik dar vienas teiginys.

The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.
Vertė grąžinta padidėjimo nėra čia naudojama, tad nėra jokio skirtumo tarp `i++` ir `++i`.
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/12-while-for/3-which-value-for/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 4

---

# Which values get shown by the "for" loop?
# Korias vertes parodo "for" ciklas?

For each loop write down which values it is going to show. Then compare with the answer.
Kiekvienam ciklui parašykite vertes, kurias jis parodys. Tada palyginkite atsakymus.

Both loops `alert` same values or not?
Abu `alert` ciklai, bet ar tos pačios vertės?

1. The postfix form:
1. Podėlio forma:

```js
for (let i = 0; i < 5; i++) alert( i );
```
2. The prefix form:
2. Priešdėlio forma:

```js
for (let i = 0; i < 5; ++i) alert( i );
Expand Down
2 changes: 1 addition & 1 deletion 1-js/02-first-steps/12-while-for/4-for-even/solution.md
Original file line number Diff line number Diff 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.
Mes naudojame "liekanos" operatorių `%`, kad gautume liekaną ir patikrintume lygybę.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/12-while-for/4-for-even/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 5

---

# Output even numbers in the loop
# Grąžinkite lyginius skaičius su ciklu

Use the `for` loop to output even numbers from `2` to `10`.
Panaudokite `for` ciklą, kad gautumėte lyginius skaičius nuo `2` iki `10`.

[demo]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
```js run
let i = 0;
while (i < 3) {
alert( `number ${i}!` );
alert( `skaičius ${i}!` );
i++;
}
```
Expand Down
6 changes: 3 additions & 3 deletions 1-js/02-first-steps/12-while-for/5-replace-for-while/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Replace "for" with "while"
# Pakeiskite "for" į "while"

Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same).
Perrašykite kodą pakeisdami ciklą `for` į `while`, nepakeisdami elgesio (turėtumėte gauti tą patį atsakymą).

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
let num;

do {
num = prompt("Enter a number greater than 100?", 0);
num = prompt("Įveskite skaičių didesnį nei 100?", 0);
} while (num <= 100 && num);
```

The loop `do..while` repeats while both checks are truthy:
Ciklas`do..while` pakartoja kol abu patikrinimai yra truthy:

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 a empty string. Then the `while` loop stops too.
1. Patikrinimas dėl `num <= 100` -- tai yra, įvesta vertė vis dar nėra didesnė už `100`.
2. Patikrinimas `&& num` yra neteisingas, jeigu `num` yra `null` arba tuščia eilutė. Tada `while` ciklas taip pat sustoja.

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. Jeigu `num` yra `null` tada `num <= 100` yra `true`, tad be antro patikrinimo ciklas nesustotų, jeigu lankytojas paspaustų CANCEL. Abu patikrinimai yra reikalingi.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 5

---

# Repeat until the input is correct
# Kartoti kol įvestis bus teisinga

Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again.
Parašykite ciklą, kurios prašytų skaičių didesnio už `100`. Jeigu lankytojas įveda kitokį skaičių -- paprašykite dar kartą įvesti.

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.
Ciklas turi prašyti skaičio tol kol lankytojas įves skaičių didesnį nei `100` arba atšauks (ang. cancels) įvestį arba paliks tuščią eilutę paspausdamas enter.

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.
Čia galime numanyti, kad lankytojas ves tik skaičius. Nėra reikalo atskirai pasirūpinti neskaitine užduoties įvestimi.

[demo]
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/12-while-for/7-list-primes/solution.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
There are many algorithms for this task.
Yra daug algoritmų šiai užduočiai.

Let's use a nested loop:
Panaudokime matrioškinį ciklą:

```js
For each i in the interval {
check if i has a divisor from 1..i
if yes => the value is not a prime
if no => the value is a prime, show it
Kiekvienam i intervale {
patikrinkite ar i turi daliklį iš 1..i
jeigu taip => vertė nėra pirminis skaičius
jeigu ne => vertė yra pirminis skaičius, parodykite ją
}
```

The code using a label:
Kodas naudojant etiketę:

```js run
let n = 10;

nextPrime:
for (let i = 2; i <= n; i++) { // for each i...
for (let i = 2; i <= n; i++) { // kiekvienam i...

for (let j = 2; j < i; j++) { // look for a divisor..
if (i % j == 0) continue nextPrime; // not a prime, go next i
for (let j = 2; j < i; j++) { // ieškoti daliklio..
if (i % j == 0) tęsti nextPrime; // ne pirminis skaičius, eiti prie sekančio i
}

alert( i ); // a prime
alert( i ); // pirminis
}
```

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.
Yra daug vietos, kur galima jį optimizuoti. Pavyzdžiui, galėtume ieškoti daliklių nuo `2` iki `i` šaknies. Bet kokiu atveju, jeigu norime būti efektyvūs dideliems intervalams, reikia pakeisti priėjimo būdus ir pasitikėti pažengusia matematika ir sudėtingais algoritmais kaip [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc.
14 changes: 7 additions & 7 deletions 1-js/02-first-steps/12-while-for/7-list-primes/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ importance: 3

---

# Output prime numbers
# Atiduoti pirminius numerius

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.
Sveikieji skaičiai didesni už `1` yra vadinami [prime](https://en.wikipedia.org/wiki/Prime_number), jeigu jie gali būti padalinti be liekanos tik iš pačio savęs arba `1`.

In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`.
Kitaip sakant, `n > 1` yra pirminis, jeigu jis negali būti lygiai padalintas iš nieko kitaip kaip tik `1` ir `n`.

For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`.
Pavyzdžiui, `5` yra pirminis, nes negali būti padalintas be liekanos iš `2`, `3` ir `4`.

**Write the code which outputs prime numbers in the interval from `2` to `n`.**
**Parašykite kodą, kuris atiduotų pirminius numerius intervalu tarp `2` iki `n`.**

For `n = 10` the result will be `2,3,5,7`.
Šiuo atveju `n = 10` rezultatas būtų `2,3,5,7`.

P.S. The code should work for any `n`, not be hard-tuned for any fixed value.
P.S. Kodas turi veikti bet kuriam `n`, o ne paruoštas atitinkamoms vertėms.
Loading