diff --git a/1-js/02-first-steps/18-while-for/1-loop-last-value/solution.md b/1-js/02-first-steps/18-while-for/1-loop-last-value/solution.md index 43ee4aad3..2e7952d89 100644 --- a/1-js/02-first-steps/18-while-for/1-loop-last-value/solution.md +++ b/1-js/02-first-steps/18-while-for/1-loop-last-value/solution.md @@ -1,4 +1,4 @@ -The answer: `1`. +Odpowiedź: `1`. ```js run let i = 3; @@ -8,18 +8,18 @@ while (i) { } ``` -Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`. +Każda obrót pętli zmniejsza `i` o `1`. Sprawdzenie `while(i)` zatrzymuje pętle kiedy `i = 0`. -Hence, the steps of the loop form the following sequence ("loop unrolled"): +Stąd kroki pętli tworzą następującą sekwencję ("pętla rozwijana"): ```js let i = 3; -alert(i--); // shows 3, decreases i to 2 +alert(i--); // pokazuje 3, zmniejsza i do 2 -alert(i--) // shows 2, decreases i to 1 +alert(i--) // pokazuje 2, zmniejsza i do 1 -alert(i--) // shows 1, decreases i to 0 +alert(i--) // pokazuje 1, zmniejsza i do 0 -// done, while(i) check stops the loop +// koniec, sprawdzenie while(i) zatrzymuje pętlę ``` diff --git a/1-js/02-first-steps/18-while-for/1-loop-last-value/task.md b/1-js/02-first-steps/18-while-for/1-loop-last-value/task.md index 3b847dfa2..9255f998d 100644 --- a/1-js/02-first-steps/18-while-for/1-loop-last-value/task.md +++ b/1-js/02-first-steps/18-while-for/1-loop-last-value/task.md @@ -1,10 +1,10 @@ -importance: 3 +ważność: 3 --- -# Last loop value +# Ostatnia wartość pętli -What is the last value alerted by this code? Why? +Jaka jest ostatnia wartość pokazana przez ten kod? Dlaczego? ```js let i = 3; diff --git a/1-js/02-first-steps/18-while-for/2-which-value-while/solution.md b/1-js/02-first-steps/18-while-for/2-which-value-while/solution.md index 495359876..ad28cd7f4 100644 --- a/1-js/02-first-steps/18-while-for/2-which-value-while/solution.md +++ b/1-js/02-first-steps/18-while-for/2-which-value-while/solution.md @@ -1,30 +1,30 @@ -The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. +Zadanie pokazuje, w jaki sposób formy postfix/prefix mogą prowadzić do różnych wyników w przypadku ich wykorzystania w porównaniach. -1. **From 1 to 4** +1. **Od 1 do 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`. + Pierwszą wartością jest `i = 1`, ponieważ `++i` najpierw powiększy `i`, a następnie zwraca nową wartość. Tak więc pierwsze porównanie jest `1 < 5`, a `alert` pokazuje `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. + Następnie podążając `2, 3, 4...` -- wartości pojawiają się jedna po drugiej. Porównanie zawsze używa zwiększonej wartości, ponieważ `++` jest przed zmienną. - 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** + W końcu, `i = 4` jest zwiększone do `5`, porównanie `while(5 < 5)` zawodzi, a pętla się zatrzymuje. Tak więc `5` nie jest pokazane. +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`). + Pierwszą wartością jest ponownie `i = 1`. Przyrostkowa forma `i++` zwiększa `i`, a następnie zwraca *starą* wartość, więc porównanie `i++ < 5` użyje `i = 0` (w przeciwieństwie do `++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`. + Ale zawołanie `alert` jest osobne. Jest to kolejna stwierdzenie, która wykonuje się po inkrementacji i porównaniu. Więc dostaje bieżący `i = 1`. - Then follow `2, 3, 4…` + Potem kolejno `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`. + Zatrzymajmy się na `i = 4`. Forma prefiksu `++i` zwiększyłaby go i użyła `5` w porównaniu. Ale tutaj mamy formę przyrostkową `i++`. Zwiększa więc `i` do `5`, ale zwraca starą wartość. Stąd porównanie `while(4 < 5)` jest prawdziwe, a kontrola przechodzi do `alert`. - The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. + Wartość `i = 5` jest ostatnią, ponieważ w następnym kroku `while(5 < 5)` jest fałszywe. diff --git a/1-js/02-first-steps/18-while-for/2-which-value-while/task.md b/1-js/02-first-steps/18-while-for/2-which-value-while/task.md index 298213237..c0e6e35c4 100644 --- a/1-js/02-first-steps/18-while-for/2-which-value-while/task.md +++ b/1-js/02-first-steps/18-while-for/2-which-value-while/task.md @@ -1,20 +1,20 @@ -importance: 4 +ważność: 4 --- -# Which values does the while loop show? +# Jakie wartości pokazuje pętla while? -For every loop iteration, write down which value it outputs and then compare it with the solution. +Dla każdej iteracji pętli zapisz, jaką wartość ona generuje, a następnie porównaj ją z rozwiązaniem. -Both loops `alert` the same values, or not? +Obie pętle ogłoszą (`alert`) te same wartości, czy nie? -1. The prefix form `++i`: +1. Forma prefiksowa `++i`: ```js let i = 0; while (++i < 5) alert( i ); ``` -2. The postfix form `i++` +2. Forma przyrostkowa (postfix) `i++` ```js let i = 0; diff --git a/1-js/02-first-steps/18-while-for/3-which-value-for/solution.md b/1-js/02-first-steps/18-while-for/3-which-value-for/solution.md index e2e28e75b..6be25c993 100644 --- a/1-js/02-first-steps/18-while-for/3-which-value-for/solution.md +++ b/1-js/02-first-steps/18-while-for/3-which-value-for/solution.md @@ -1,4 +1,4 @@ -**The answer: from `0` to `4` in both cases.** +**Odpowiedź: od `0` do `4` w obu przypadkach.** ```js run for (let i = 0; i < 5; ++i) alert( i ); @@ -6,12 +6,11 @@ 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`: +Można to łatwo odczytać z algorytmu `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. Wykonać raz `i = 0` przed wszystkim (początek). +2. Sprawdzić warunek `i < 5` +3. jeśli `true` -- wykonaj ciało pętli `alert(i)`, a potem `i++` -The increment `i++` is separated from the condition check (2). That's just another statement. - -The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. +Przyrost `i++` jest oddzielony od sprawdzenia warunku (2). To tylko kolejne stwierdzenie. +Wartość zwracana przez przyrost nie jest tutaj używana, więc nie ma różnicy między `i++` a `++i`. diff --git a/1-js/02-first-steps/18-while-for/3-which-value-for/task.md b/1-js/02-first-steps/18-while-for/3-which-value-for/task.md index bfefa63f5..0ad095ed6 100644 --- a/1-js/02-first-steps/18-while-for/3-which-value-for/task.md +++ b/1-js/02-first-steps/18-while-for/3-which-value-for/task.md @@ -1,19 +1,19 @@ -importance: 4 +ważność: 4 --- -# Which values get shown by the "for" loop? +# Jakie wartości pokazuje pętla "for"? -For each loop write down which values it is going to show. Then compare with the answer. +Dla każdej pętli zapisz, jakie wartości ma ona pokazywać. Następnie porównaj z odpowiedzią. -Both loops `alert` same values or not? +Obie pętle ogłaszają (`alert`) te same wartości, czy nie? -1. The postfix form: +1. Forma przyrostkowa: ```js for (let i = 0; i < 5; i++) alert( i ); ``` -2. The prefix form: +2. Forma prefixowa: ```js for (let i = 0; i < 5; ++i) alert( i ); diff --git a/1-js/02-first-steps/18-while-for/4-for-even/solution.md b/1-js/02-first-steps/18-while-for/4-for-even/solution.md index e8e66bb47..cfd4e55b4 100644 --- a/1-js/02-first-steps/18-while-for/4-for-even/solution.md +++ b/1-js/02-first-steps/18-while-for/4-for-even/solution.md @@ -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. +Używamy operatora "modulo" `%` aby uzyskać resztę i sprawdzić tutaj równomierność. diff --git a/1-js/02-first-steps/18-while-for/4-for-even/task.md b/1-js/02-first-steps/18-while-for/4-for-even/task.md index ff34e7e40..f84494954 100644 --- a/1-js/02-first-steps/18-while-for/4-for-even/task.md +++ b/1-js/02-first-steps/18-while-for/4-for-even/task.md @@ -1,9 +1,9 @@ -importance: 5 +ważność: 5 --- -# Output even numbers in the loop +# Zwróć numery parzyste w pętli -Use the `for` loop to output even numbers from `2` to `10`. +Użyj pętli `for` do wyprowadzenia parzystych liczb od `2` do `10`. [demo] diff --git a/1-js/02-first-steps/18-while-for/5-replace-for-while/solution.md b/1-js/02-first-steps/18-while-for/5-replace-for-while/solution.md index 612cf559c..d0f026321 100644 --- a/1-js/02-first-steps/18-while-for/5-replace-for-while/solution.md +++ b/1-js/02-first-steps/18-while-for/5-replace-for-while/solution.md @@ -3,7 +3,7 @@ ```js run let i = 0; while (i < 3) { - alert( `number ${i}!` ); + alert( `liczba ${i}!` ); i++; } ``` diff --git a/1-js/02-first-steps/18-while-for/5-replace-for-while/task.md b/1-js/02-first-steps/18-while-for/5-replace-for-while/task.md index 0c69d9c2d..71bd94fc8 100644 --- a/1-js/02-first-steps/18-while-for/5-replace-for-while/task.md +++ b/1-js/02-first-steps/18-while-for/5-replace-for-while/task.md @@ -1,14 +1,14 @@ -importance: 5 +ważność: 5 --- -# Replace "for" with "while" +# Zastąp "for" pętlą "while" -Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same). +Przepisz ponownie kod zmieniając pętlę `for` na `while` bez zmiany jej zachowania (wyjście powinno pozostać takie samo). ```js run for (let i = 0; i < 3; i++) { - alert( `number ${i}!` ); + alert( `liczba ${i}!` ); } ``` diff --git a/1-js/02-first-steps/18-while-for/6-repeat-until-correct/solution.md b/1-js/02-first-steps/18-while-for/6-repeat-until-correct/solution.md index 2e04a78c4..3690c0c69 100644 --- a/1-js/02-first-steps/18-while-for/6-repeat-until-correct/solution.md +++ b/1-js/02-first-steps/18-while-for/6-repeat-until-correct/solution.md @@ -3,13 +3,13 @@ let num; do { - num = prompt("Enter a number greater than 100?", 0); + num = prompt("Wprowadź liczbę większą niż 100?", 0); } while (num <= 100 && num); ``` -The loop `do..while` repeats while both checks are truthy: +Pętla `do...while` powtarza się, podczas gdy oba sprawdzenia są prawdziwe: -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. 1. Sprawdzenie, czy `num <= 100` -- to znaczy, że wprowadzona wartość jest wciąż nie większa niż `100`. +2. Sprawdzanie `&& num` jest fałszywe, gdy `num` ma wartość `null` lub jest pustym stringiem. Wtedy pętla `while` też się zatrzymuje. -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. Jeśli `num` ma wartość `null` to `num <= 100` ma wartość `true`, więc bez drugiego sprawdzenia pętla nie zatrzymałaby się, gdyby użytkownik kliknął PRZERWIJ. Oba sprawdzenia są wymagane. diff --git a/1-js/02-first-steps/18-while-for/6-repeat-until-correct/task.md b/1-js/02-first-steps/18-while-for/6-repeat-until-correct/task.md index 0788ee76e..ad747dda9 100644 --- a/1-js/02-first-steps/18-while-for/6-repeat-until-correct/task.md +++ b/1-js/02-first-steps/18-while-for/6-repeat-until-correct/task.md @@ -1,13 +1,13 @@ -importance: 5 +ważność: 5 --- -# Repeat until the input is correct +# Powtarzaj do momentu, aż wejście będzie prawidłowe -Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again. +Napisz pętlę, która pyta o liczbę większą niż `100`. Jeśli odwiedzający wprowadzi inną liczbę - poproś go o jej ponowne wprowadzenie. -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. +Pętla musi pytać o liczbę, dopóki użytkownik nie wprowadzi liczby większej niż `100` lub nie anuluje wejścia/wprowadzi pustą linię. -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. +Tutaj możemy założyć, że odwiedzający wprowadza tylko numery. Nie ma potrzeby implementowania specjalnej obsługi dla nie-numerycznych danych wejściowych w tym zadaniu. [demo] diff --git a/1-js/02-first-steps/18-while-for/7-list-primes/solution.md b/1-js/02-first-steps/18-while-for/7-list-primes/solution.md index b4b64b6fa..b98d443e3 100644 --- a/1-js/02-first-steps/18-while-for/7-list-primes/solution.md +++ b/1-js/02-first-steps/18-while-for/7-list-primes/solution.md @@ -1,29 +1,29 @@ -There are many algorithms for this task. +Istnieje wiele algorytmów dla tego zadania. -Let's use a nested loop: +Użyjmy pętli zagnieżdżonej: ```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 +Dla każdego i w przedziale { + sprawdź jeśli i ma dzielnik od 1..i + jeśli tak => wartość nie jest liczbą pierwszą + jeśli nie => wartość jest liczbą pierwszą, pokaż ją } ``` -The code using a label: +Kod używając etykiety: ```js run let n = 10; nextPrime: -for (let i = 2; i <= n; i++) { // for each i... +for (let i = 2; i <= n; i++) { // dla każdego 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++) { // szukaj dziewlnika.. + if (i % j == 0) continue nextPrime; // nie liczba pierwsza idź do następnego i } - alert( i ); // a prime + alert( i ); // liczba pierwsza } ``` -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. +Jest dużo miejsca na optymalizację. Na przykład, możemy szukać dzielników od `2` do pierwiastka kwadratowego `i`. Ale tak czy inaczej, jeśli chcemy być naprawdę wydajni w dużych odstępach czasu, musimy zmienić podejście i polegać na zaawansowanych matematykach i złożonych algorytmach, takich jak [Sito kwadratowe](https://pl.wikipedia.org/wiki/Sito_kwadratowe), [Ogólne sito ciała liczbowego](https://pl.wikipedia.org/wiki/GNFS) itd. diff --git a/1-js/02-first-steps/18-while-for/7-list-primes/task.md b/1-js/02-first-steps/18-while-for/7-list-primes/task.md index 6344b9f6f..66a7e7f75 100644 --- a/1-js/02-first-steps/18-while-for/7-list-primes/task.md +++ b/1-js/02-first-steps/18-while-for/7-list-primes/task.md @@ -1,17 +1,17 @@ -importance: 3 +ważność: 3 --- -# Output prime numbers +# Zwróć liczby pierwsze -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. +Liczba całkowita większa od `1` nazywana jest [liczbą pierwszą](https://pl.wikipedia.org/wiki/Liczba_pierwsza), jeśli nie może być podzielona bez reszty przez nic poza `1` i samą siebie. -In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`. +Innymi słowy, `n > 1` jest liczbą pierwszą, jeśli nie można jej równo podzielić przez nic poza `1` i `n`. -For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`. +Na przykład, `5` jest liczbą pierwszą, ponieważ nie można jej podzielić bez reszty przez `2`, `3` i `4`. -**Write the code which outputs prime numbers in the interval from `2` to `n`.** +**Napisz kod, który wyprowadza liczby pierwsze w przedziale od `2` do `n`.** -For `n = 10` the result will be `2,3,5,7`. +Dla `n = 10` wynik będzie `2,3,5,7`. -P.S. The code should work for any `n`, not be hard-tuned for any fixed value. +P.S. Kod powinien działać dla każdego `n`, nie powinien być dostosowany na stałe do żadnej wartości. diff --git a/1-js/02-first-steps/18-while-for/article.md b/1-js/02-first-steps/18-while-for/article.md index 382adadac..e85b5b1b3 100644 --- a/1-js/02-first-steps/18-while-for/article.md +++ b/1-js/02-first-steps/18-while-for/article.md @@ -1,54 +1,54 @@ -# Loops: while and for +# Pętle: while i for -We often need to repeat actions. +Często musimy powtarzać działania. -For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. +Na przykład, wysyłanie towarów z listy jeden po drugim lub po prostu uruchamianie tego samego kodu dla każdej liczby od 1 do 10. -*Loops* are a way to repeat the same code multiple times. +*Pętle* są sposobem na wielokrotne powtarzanie tego samego kodu. -## The "while" loop +## Pętla "while" -The `while` loop has the following syntax: +Pętla `while` ma następującą składnię:: ```js -while (condition) { - // code - // so-called "loop body" +while (warunek) { + // kod + // tak zwane "ciało pętli" } ``` -While the `condition` is truthy, the `code` from the loop body is executed. +Podczas gdy `warunek` jest prawdą, `kod` z ciała pętli jest wykonywany. -For instance, the loop below outputs `i` while `i < 3`: +Na przykład pętla poniżej wysyła `i` dopóki `i < 3`: ```js run let i = 0; -while (i < 3) { // shows 0, then 1, then 2 +while (i < 3) { // pokazuje 0, następnie 1, następnie 2 alert( i ); i++; } ``` -A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations. +Pojedyńcze wykonanie ciała pętli jest nazywane *iteracją*. Pętla w powyższym przykładzie wykonuje trzy iteracje. -If `i++` was missing from the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and in server-side JavaScript, we can kill the process. +Gdyby w powyższym przykładzie brakowało `i++`, pętla powtarzałaby się (w teorii) wiecznie. W praktyce, przeglądarka dostarcza sposobów na zatrzymanie takich pętli, a w JavaScript po stronie serwera, możemy zabić proces. -Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by `while`. +Każde wyrażenie lub zmienna może być warunkiem pętli, nie tylko porównanie: warunek jest oceniany i zamieniany na boolean przez `while`. -For instance, a shorter way to write `while (i != 0)` is `while (i)`: +Na przykład, krótszym sposobem na napisanie `while (i != 0)` jest `while (i)`: ```js run let i = 3; *!* -while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops +while (i) { // kiedy i staje się 0, warunek staje się fałszywy i pętla się zatrzymuje */!* alert( i ); i--; } ``` -````smart header="Curly braces are not required for a single-line body" -If the loop body has a single statement, we can omit the curly braces `{…}`: +````smart header="Nawiasy klamrowe nie są wymagane dla jedno-linijkowego ciała" +Jeśli ciało pętli ma jedno wyrażenie, możemy pominąć nawiasy klamrowe `{…}`: ```js run let i = 3; @@ -58,19 +58,19 @@ while (i) alert(i--); ``` ```` -## The "do..while" loop +## Pętla "do..while" -The condition check can be moved *below* the loop body using the `do..while` syntax: +Kontrola warunku może być przesunięta *poniżej* ciała pętli za pomocą składni `do...while`: ```js do { - // loop body -} while (condition); + // ciało pętli +} while (warunek); ``` -The loop will first execute the body, then check the condition, and, while it's truthy, execute it again and again. +Pętla najpierw wykona ciało, następnie sprawdzi warunek i jeśli jest prawdziwy wykona je ponownie i ponownie. -For example: +Na przykład: ```js run let i = 0; @@ -80,109 +80,109 @@ do { } while (i < 3); ``` -This form of syntax should only be used when you want the body of the loop to execute **at least once** regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`. +Ta forma składni powinna być stosowana tylko wtedy, gdy chcemy, aby ciało pętli wykonało się **przynajmniej raz** niezależnie od tego, czy warunek jest prawdziwy. Zazwyczaj preferowana jest druga forma: `while(...) {...}`. -## The "for" loop +## Pętla "for" -The `for` loop is more complex, but it's also the most commonly used loop. +Pętla `for` jest bardziej złożona, ale jest to również najczęściej używana pętla. -It looks like this: +Wygląda tak: ```js -for (begin; condition; step) { - // ... loop body ... +for (początek; warunek; krok) { + // ... ciało pętli ... } ``` -Let's learn the meaning of these parts by example. The loop below runs `alert(i)` for `i` from `0` up to (but not including) `3`: +Poznajmy znaczenie tych części na przykładzie. Poniższa pętla uruchomi `alert(i)` dla `i` od `0` do (ale nie włączając) `3`: ```js run -for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2 +for (let i = 0; i < 3; i++) { // pokazuje 0, następnie 1, następnie 2 alert(i); } ``` -Let's examine the `for` statement part-by-part: +Zbadajmy wyrażenie `for` kawałek po kawałku: -| part | | | +| część | | | |-------|----------|----------------------------------------------------------------------------| -| begin | `i = 0` | Executes once upon entering the loop. | -| condition | `i < 3`| Checked before every loop iteration. If false, the loop stops. | -| body | `alert(i)`| Runs again and again while the condition is truthy. | -| step| `i++` | Executes after the body on each iteration. | +| początek | `i = 0` | Wykonuje się raz po wejściu do pętli. | +| warunek | `i < 3`| Sprawdzane przed każdą iteracją pętli. Jeśli fałsz, pętla się zatrzymuje. | +| ciało | `alert(i)`| Uruchamiane w kółko, gdy warunek jest prawdziwy. | +| krok | `i++` | Wykonuje się po ciele na każdej iteracji. | -The general loop algorithm works like this: +Ogólny algorytm pętli działa w ten sposób: ``` -Run begin -→ (if condition → run body and run step) -→ (if condition → run body and run step) -→ (if condition → run body and run step) +Początek uruchomienia +→ (jeśli warunek → uruchamia ciało i krok) +→ (jeśli warunek → uruchamia ciało i krok) +→ (jeśli warunek → uruchamia ciało i krok) → ... ``` -That is, `begin` executes once, and then it iterates: after each `condition` test, `body` and `step` are executed. +To znaczy, `zacznij` wykonywać raz, a następnie iteruje: po każdym sprawdzeniu `warunku`, `ciało` i `krok` są wykonywane. -If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper. +Jeśli jesteś nowym użytkownikiem pętli, możesz wrócić do tego przykładu i odtworzyć, jak przebiega on krok po kroku na kartce papieru. -Here's exactly what happens in our case: +Oto co dokładnie dzieje się w naszym przypadku: ```js // for (let i = 0; i < 3; i++) alert(i) -// run begin +// uruchomienie się rozpoczyna let i = 0 -// if condition → run body and run step +// jeśli warunek spełniony → uruchom ciało i krok if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// jeśli warunek spełniony → uruchom ciało i krok if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// jeśli warunek spełniony → uruchom ciało i krok if (i < 3) { alert(i); i++ } -// ...finish, because now i == 3 +// ...kończy, ponieważ teraz i == 3 ``` -````smart header="Inline variable declaration" -Here, the "counter" variable `i` is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop. +````smart header="Deklaracja zmiennej w linii" +Tutaj, zmienna "licznik" `i` jest deklarowana w pętli. Nazywa się to deklaracją zmiennej "w linii" (inline). Takie zmienne są widoczne tylko wewnątrz pętli. ```js run for (*!*let*/!* i = 0; i < 3; i++) { alert(i); // 0, 1, 2 } -alert(i); // error, no such variable +alert(i); // błąd, nie ma takiej zmiennej ``` -Instead of defining a variable, we could use an existing one: +Zamiast definiować zmienną, moglibyśmy użyć istniejącej: ```js run let i = 0; -for (i = 0; i < 3; i++) { // use an existing variable +for (i = 0; i < 3; i++) { // użyj istniejącej zmiennej alert(i); // 0, 1, 2 } -alert(i); // 3, visible, because declared outside of the loop +alert(i); // 3, widoczne, ponieważ zadeklarowane poza pętlą ``` ```` -### Skipping parts +### Pomijanie części -Any part of `for` can be skipped. +Każda część `for` może być pominięta. -For example, we can omit `begin` if we don't need to do anything at the loop start. +Na przykład, możemy pominąć `początek`, jeśli nie musimy nic robić na początku pętli. -Like here: +Jak tutaj: ```js run -let i = 0; // we have i already declared and assigned +let i = 0; // mamy i już zadeklarowane i przypisane -for (; i < 3; i++) { // no need for "begin" +for (; i < 3; i++) { // nie ma potrzeby "początku" alert( i ); // 0, 1, 2 } ``` -We can also remove the `step` part: +Możemy także usunąć część `krok`: ```js run let i = 0; @@ -192,32 +192,34 @@ for (; i < 3;) { } ``` -This makes the loop identical to `while (i < 3)`. +To czyni pętlę identyczną jak `while (i < 3)`. -We can actually remove everything, creating an infinite loop: +Możemy właściwie usunąć wszystko, tworząc nieskończoną pętlę: ```js for (;;) { - // repeats without limits + // powtarza się bez ograniczeń } ``` Please note that the two `for` semicolons `;` must be present. Otherwise, there would be a syntax error. +Proszę zwrócić uwagę, że te dwa średniki `;` pętli `for` muszą być obecne. W przeciwnym razie wystąpiłby błąd składniowy. -## Breaking the loop +## Przerywanie pętli -Normally, a loop exits when its condition becomes falsy. +Normalnie, pętla wychodzi, gdy jej warunek staje się fałszywy. -But we can force the exit at any time using the special `break` directive. +Ale w każdej chwili możemy wymusić wyjście za pomocą specjalnej dyrektywy `break`. For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered: +Na przykład, poniższa pętla pyta użytkownika o serię numerów, "przerywa" (break), gdy nie jest wprowadzona żadna liczba: ```js run let sum = 0; while (true) { - let value = +prompt("Enter a number", ''); + let value = +prompt("Wprowadź liczbę", ''); *!* if (!value) break; // (*) @@ -226,35 +228,35 @@ while (true) { sum += value; } -alert( 'Sum: ' + sum ); +alert( 'Suma: ' + sum ); ``` -The `break` directive is activated at the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, `alert`. +Dyrektywa `break` jest aktywowana w wierszu `(*)`, jeśli użytkownik wprowadzi pusty wiersz lub anuluje wejście. Zatrzymuje ona natychmiast pętlę, przekazując sterowanie do pierwszego wiersza po pętli. Mianowicie, `alert`. -The combination "infinite loop + `break` as needed" is great for situations when a loop's condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body. +Kombinacja "nieskończona pętla + `break`(przerwanie) w razie potrzeby" jest świetna w sytuacjach, gdy stan pętli musi być sprawdzony nie na początku lub na końcu pętli, ale w środku lub nawet w kilku miejscach jej ciała. -## Continue to the next iteration [#continue] +## Kontynuuj do następnej iteracji [#continue] -The `continue` directive is a "lighter version" of `break`. It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows). +Dyrektywa `continue`(kontynuuj) jest "lżejszą wersją" `break`(przerwania). Nie zatrzymuje ona całej pętli. Zamiast tego, zatrzymuje bieżącą iterację i zmusza pętlę do rozpoczęcia nowej (jeśli warunek na to pozwala). -We can use it if we're done with the current iteration and would like to move on to the next one. +Możemy go użyć, jeśli skończyliśmy z obecną iteracją i chcielibyśmy przejść do następnej. -The loop below uses `continue` to output only odd values: +Poniższa pętla używa `continue` do wyprowadzania tylko nieparzystych wartości: ```js run no-beautify for (let i = 0; i < 10; i++) { - // if true, skip the remaining part of the body + // jeśli prawda, pomiń pozostałą część ciała *!*if (i % 2 == 0) continue;*/!* - alert(i); // 1, then 3, 5, 7, 9 + alert(i); // 1, następnie 3, 5, 7, 9 } ``` -For even values of `i`, the `continue` directive stops executing the body and passes control to the next iteration of `for` (with the next number). So the `alert` is only called for odd values. +Dla wartości parzystych `i`, dyrektywa `continue` przestaje wykonywać ciało i przekazuje kontrolę do następnej iteracji `for` (z kolejnym numerem). Tak więc `alert` jest wywoływany tylko dla wartości nieparzystych. -````smart header="The `continue` directive helps decrease nesting" -A loop that shows odd values could look like this: +````smart header="Dyrektywa `continue` pomaga zmniejszyć zagnieżdżanie" +Pętla, która pokazuje nieparzyste wartości, może wyglądać tak: ```js for (let i = 0; i < 10; i++) { @@ -266,15 +268,15 @@ for (let i = 0; i < 10; i++) { } ``` -From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an `if` block instead of using `continue`. +Z technicznego punktu widzenia, jest to identyczne z powyższym przykładem. Z pewnością możemy po prostu zawinąć kod w bloku `if` zamiast używać `continue`. -But as a side-effect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of`if` is longer than a few lines, that may decrease the overall readability. +Ale jako efekt uboczny, stworzyło to jeszcze jeden poziom zagnieżdżenia (`alert` wywołany wewnątrz nawiasów klamrowych). Jeśli kod wewnątrz `if` jest dłuższy niż kilka linii, może to zmniejszyć ogólną czytelność. ```` -````warn header="No `break/continue` to the right side of '?'" -Please note that syntax constructs that are not expressions cannot be used with the ternary operator `?`. In particular, directives such as `break/continue` aren't allowed there. +````warn header="Nie `break/continue` po prawej stronie '?'" +Proszę zauważyć, że konstrukcje składniowe, które nie są wyrażeniami, nie mogą być używane z operatorem trójdzielnym `?`. W szczególności, dyrektywy takie jak `break/continue` nie są tam dozwolone. -For example, if we take this code: +Na przykład, jeśli weźmiemy ten kod: ```js if (i > 5) { @@ -284,103 +286,101 @@ if (i > 5) { } ``` -...and rewrite it using a question mark: - +...i przepiszemy go używając znaku zapytania: ```js no-beautify -(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here +(i > 5) ? alert(i) : *!*continue*/!*; // continue nie jest tu dozwolone ``` -...it stops working: there's a syntax error. +...przestaje działać: jest błąd składniowy. -This is just another reason not to use the question mark operator `?` instead of `if`. +Jest to kolejny powód, aby nie używać operatora znaku zapytania `?` zamiast `if`. ```` -## Labels for break/continue +## Etykiety dla break/continue (labels) -Sometimes we need to break out from multiple nested loops at once. +Czasami musimy się wyrwać z wielu zagnieżdżonych pętli naraz. -For example, in the code below we loop over `i` and `j`, prompting for the coordinates `(i, j)` from `(0,0)` to `(2,2)`: +Dla przykładu, w poniższym kodzie iterujemy po `i` i `j`, pytając o współrzędne `(i, j)` od `(0,0)` do `(2,2)`: ```js run no-beautify for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Wartości współrzędnych (${i},${j})`, ''); - // what if we want to exit from here to Done (below)? + // co jeśli chcemy wyjść stąd do Zrobione (poniżej)? } } -alert('Done!'); +alert('Zrobione!'); ``` -We need a way to stop the process if the user cancels the input. +Potrzebujemy sposobu na zatrzymanie procesu, jeśli użytkownik anuluje wprowadzanie. -The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue! +Zwykłe `break` po `input` przerwałby tylko wewnętrzną pętlę. To nie wystarczy... etykiety, przyjdźcie na ratunek! -A *label* is an identifier with a colon before a loop: +*Etykieta* to identyfikator z dwukropkiem przed pętlą: ```js -labelName: for (...) { +nazwaEtykiety: for (...) { ... } ``` -The `break ` statement in the loop below breaks out to the label: +Stwierdzenie `break ` w pętli poniżej wychodzi do etykiety: ```js run no-beautify *!*outer:*/!* for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Wartość współrzędnych (${i},${j})`, ''); - // if an empty string or canceled, then break out of both loops + // jeśli pusty string lub anulowano, wówczas wyjdź z obu pętli if (!input) *!*break outer*/!*; // (*) - // do something with the value... + // zrób coś z wartością... } } -alert('Done!'); +alert('Zrobione!'); ``` -In the code above, `break outer` looks upwards for the label named `outer` and breaks out of that loop. - -So the control goes straight from `(*)` to `alert('Done!')`. +W powyższym kodzie, `break outer` patrzy do góry na etykietę o nazwie `outer` i wychodzi z tej pętli. +Więc sterowanie idzie prosto z `(*)` do `alert('Zrobione!')`. -We can also move the label onto a separate line: +Możemy również przenieść etykietę na osobną linię: ```js no-beautify outer: for (let i = 0; i < 3; i++) { ... } ``` -The `continue` directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop. +Dyrektywa `continue` może być również stosowana z etykietą. W tym przypadku wykonanie kodu przeskakuje do następnej iteracji etykietowanej pętli. -````warn header="Labels do not allow to \"jump\" anywhere" -Labels do not allow us to jump into an arbitrary place in the code. +````warn header="Etykiety nie pozwalają \"skakać\" gdziekolwiek" +Etykiety nie pozwalają nam wskoczyć w dowolne miejsce w kodzie. -For example, it is impossible to do this: +Na przykład, nie da się tego zrobić: ```js -break label; // doesn't jumps to the label below +break label; // nie przeskakuje do etykiety poniżej label: for (...) ``` -A call to `break/continue` is only possible from inside a loop and the label must be somewhere above the directive. +Wezwanie do `break/continue` jest możliwe tylko z wnętrza pętli i etykieta musi być gdzieś nad dyrektywą. ```` -## Summary +## Podsumowanie -We covered 3 types of loops: +Poznaliśmy 3 rodzaje pętli: -- `while` -- The condition is checked before each iteration. -- `do..while` -- The condition is checked after each iteration. -- `for (;;)` -- The condition is checked before each iteration, additional settings available. +- `while` -- Warunek jest sprawdzany przed każdą iteracją. +- `do..while` -- Warunek jest sprawdzany po każdej iteracji. +- `for (;;)` -- Warunek jest sprawdzany przed każdą iteracją, dostępne są dodatkowe ustawienia. -To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive. +Aby zrobić "nieskończoną" pętlę, zwykle używa się konstrukcji`while(true)`. Taką pętlę, tak jak każdą inną, można zatrzymać za pomocą dyrektywy `break`. -If we don't want to do anything in the current iteration and would like to forward to the next one, we can use the `continue` directive. +Jeśli nie chcemy nic robić w obecnej iteracji i chcielibyśmy przejść do następnej, możemy skorzystać z dyrektywy `continue`. -`break/continue` support labels before the loop. A label is the only way for `break/continue` to escape a nested loop to go to an outer one. +`break/continue` wspierają etykiety przed pętlą. Etykieta jest jedynym sposobem dla `break/continue`, aby uciec z zagnieżdżonej pętli i przejść do zewnętrznej. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy