From edb67f3f0ac5956968af3e5e72b0b66380213211 Mon Sep 17 00:00:00 2001 From: Anang Fachreza Date: Tue, 27 Aug 2019 16:55:08 +0700 Subject: [PATCH] translate loops: while and for --- .../1-loop-last-value/solution.md | 14 +- .../12-while-for/1-loop-last-value/task.md | 6 +- .../2-which-value-while/solution.md | 22 +- .../12-while-for/2-which-value-while/task.md | 12 +- .../3-which-value-for/solution.md | 14 +- .../12-while-for/3-which-value-for/task.md | 12 +- .../12-while-for/4-for-even/solution.md | 2 +- .../12-while-for/4-for-even/task.md | 6 +- .../12-while-for/5-replace-for-while/task.md | 6 +- .../6-repeat-until-correct/solution.md | 10 +- .../6-repeat-until-correct/task.md | 10 +- .../12-while-for/7-list-primes/solution.md | 24 +- .../12-while-for/7-list-primes/task.md | 16 +- 1-js/02-first-steps/12-while-for/article.md | 240 +++++++++--------- 14 files changed, 196 insertions(+), 198 deletions(-) diff --git a/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md b/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md index 43ee4aad3..e491d949e 100644 --- a/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md +++ b/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md @@ -1,4 +1,4 @@ -The answer: `1`. +Jawabanya: `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`. +Setiap pengulangan mengurangi `i` oleh `1`. Cek `while(i)` menghentikan perulangan ketika `i = 0`. -Hence, the steps of the loop form the following sequence ("loop unrolled"): +Oleh karena itu, langkah-langkah perulangan membentuk urutan berikut ("perulangan terbuka"): ```js let i = 3; -alert(i--); // shows 3, decreases i to 2 +alert(i--); // menampilkan 3, mengurangi i ke 2 -alert(i--) // shows 2, decreases i to 1 +alert(i--) // menampilkan 2, mengurangi i ke 1 -alert(i--) // shows 1, decreases i to 0 +alert(i--) // menampilkan 1, mengurangi i ke 0 -// done, while(i) check stops the loop +// selesai while(i) cek menghentikan pengulangan ``` diff --git a/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md b/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md index 3b847dfa2..92ad1566b 100644 --- a/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md +++ b/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md @@ -1,10 +1,10 @@ -importance: 3 +nilai penting: 3 --- -# Last loop value +# Nilai terakhir perulangan -What is the last value alerted by this code? Why? +Apa nilai terakhir yang diperingatkan oleh kode ini? Mengapa? ```js let i = 3; diff --git a/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md b/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md index 495359876..79a37528d 100644 --- a/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md +++ b/1-js/02-first-steps/12-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. +Tugas mendemonstrasikan bagaimana bentuk postfix/prefix dapat menyebabkan hasil yang berbeda ketika digunakan dalam perbandingan -1. **From 1 to 4** +1. **Dari 1 ke 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`. + nilai pertama adalah `i = 1`, karena `++i` pertama menaikan `i` dan mengembalikan nilai baru. Jadi perbandingan pertama adalah `1 < 5` dan `alert` menampilkan `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. + lalu diikuti `2, 3, 4…` -- nilainya muncul satu per satu. Perbandingan selalu menggunakan nilai yang ditambah, karna `++` sebelum variabel. - 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** + Akhirnya, `i = 4` naik menjadi `5`, perbandingan `while(5 < 5)` gagal, dan pengulangan berhenti. Jadi `5` tidak ditampilkan. +2. **Dari 1 ke 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`). + Lagi, nilai pertama adalah `i = 1`. bentuk postfix dari `i++` menaikan `i` lalu mengembalikan nilai yang *lama*, jadi perbandinganya `i++ < 5` akan menggunakan `i = 0` (berbeda dengan `++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`. + Namun panggilan `alert` terpisah. ini adalah pernyataan lain yang berjalan setelah kenaikan dan perbandingan. Jadi ini mendapatkan yang sekarang `i = 1`. - Then follow `2, 3, 4…` + Lalu diikuti `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`. + Mari berhenti di `i = 4`. bentuk prefix `++i` akan menaikannya dan menggunakan `5` di perbandingan. Tapi disini kita mempunyai bentuk postfix `i++`. jadi ini menaikan `i` ke `5`, namun mengembalikan nilai yang lama. Karna perbandingan yang sebenarnya adalah `while(4 < 5)` -- benar, dan kontrol berlanjut ke `alert`. - The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. + Nilai `i = 5` adalah yang terkahir, karena pada langkah berikutnya `while(5 < 5)` adalah salah. diff --git a/1-js/02-first-steps/12-while-for/2-which-value-while/task.md b/1-js/02-first-steps/12-while-for/2-which-value-while/task.md index 298213237..ca41b7e9f 100644 --- a/1-js/02-first-steps/12-while-for/2-which-value-while/task.md +++ b/1-js/02-first-steps/12-while-for/2-which-value-while/task.md @@ -1,20 +1,20 @@ -importance: 4 +nilai penting: 4 --- -# Which values does the while loop show? +# Nilai mana yang ditampilkan perulangan while? -For every loop iteration, write down which value it outputs and then compare it with the solution. +Setiap perulangan, tulis nilai yang dikeluarkan dan bandingkan dengan solusinya. -Both loops `alert` the same values, or not? +Kedua perulangan `alert` nilai yang sama, atau tidak? -1. The prefix form `++i`: +1. Bentuk prefix `++i`: ```js let i = 0; while (++i < 5) alert( i ); ``` -2. The postfix form `i++` +2. Bentuk postfix `i++` ```js let i = 0; diff --git a/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md b/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md index e2e28e75b..b96eaf8c3 100644 --- a/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md +++ b/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md @@ -1,4 +1,4 @@ -**The answer: from `0` to `4` in both cases.** +**Jawabanya: dari `0` ke `4` di kedua kasus.** ```js run for (let i = 0; i < 5; ++i) alert( i ); @@ -6,12 +6,12 @@ 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`: +Itu dapat dengan mudah dikurangkan dari algoritma dari `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. Jalankan sekali `i = 0` sebelum apapun (mulai). +2. Cek kondisinya `i < 5` +3. Jika `true` -- jalankan badan perulangan `alert(i)`, lalu `i++` -The increment `i++` is separated from the condition check (2). That's just another statement. +Kenaikan `i++` terpisah dari pengecekan kondisi (2). itu hanya pernyataan lain. -The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. +Nilai yang dikembalikan oleh kenaikan tidak digunakan disini, jadi tidak ada bedanya antara `i++` dan `++i`. diff --git a/1-js/02-first-steps/12-while-for/3-which-value-for/task.md b/1-js/02-first-steps/12-while-for/3-which-value-for/task.md index bfefa63f5..69d7f22be 100644 --- a/1-js/02-first-steps/12-while-for/3-which-value-for/task.md +++ b/1-js/02-first-steps/12-while-for/3-which-value-for/task.md @@ -1,19 +1,19 @@ -importance: 4 +nilai penting: 4 --- -# Which values get shown by the "for" loop? +# Nilai mana yang ditampilkan oleh perulangan "for" ? -For each loop write down which values it is going to show. Then compare with the answer. +Untuk setiap perulangan tulis nilai mana yang akan ditampilkan. lalu bandingkan dengan jawabanya -Both loops `alert` same values or not? +Kedua perulangan `alert` nilai yang sama atau tidak? -1. The postfix form: +1. Bentuk postfix: ```js for (let i = 0; i < 5; i++) alert( i ); ``` -2. The prefix form: +2. Bentuk prefix: ```js for (let i = 0; i < 5; ++i) alert( i ); diff --git a/1-js/02-first-steps/12-while-for/4-for-even/solution.md b/1-js/02-first-steps/12-while-for/4-for-even/solution.md index e8e66bb47..ceed84702 100644 --- a/1-js/02-first-steps/12-while-for/4-for-even/solution.md +++ b/1-js/02-first-steps/12-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. +Kita menggunakan operator "modulo" `%` untuk mendapatkan sisanya dan untuk mengecek kegenapan disini. diff --git a/1-js/02-first-steps/12-while-for/4-for-even/task.md b/1-js/02-first-steps/12-while-for/4-for-even/task.md index ff34e7e40..601bb003c 100644 --- a/1-js/02-first-steps/12-while-for/4-for-even/task.md +++ b/1-js/02-first-steps/12-while-for/4-for-even/task.md @@ -1,9 +1,9 @@ -importance: 5 +nilai penting: 5 --- -# Output even numbers in the loop +# Menghasilkan angka genap di perulangan -Use the `for` loop to output even numbers from `2` to `10`. +Gunakan perulangan `for` untuk menghasilkan angka genap dari `2`sampai `10`. [demo] diff --git a/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md b/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md index 0c69d9c2d..59dfc1398 100644 --- a/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md +++ b/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md @@ -1,10 +1,10 @@ -importance: 5 +nilai penting: 5 --- -# Replace "for" with "while" +# Ganti "for" dengan "while" -Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same). +Tulis ulang kode ubah perulangan `for` ke `while` tanpa mengubah perlakunya (hasilnya harus tetap sama). ```js run for (let i = 0; i < 3; i++) { diff --git a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md index 2e04a78c4..2bb2a0c18 100644 --- a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md +++ b/1-js/02-first-steps/12-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("Masukan angka lebih dari 100?", 0); } while (num <= 100 && num); ``` -The loop `do..while` repeats while both checks are truthy: +Perulangan `do..while` diulangi selagi kedua cek itu bernilai benar: -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. Cek untuk `num <= 100` -- itu adalah, nilai yang dimasukan masih tidak lebih besar dari `100`. +2. Cek `&& num` adalah salah ketika `num` adalah `null` atau sebuah string kosong. lalu perulangan `while` berhenti juga. -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. Jika `num` adalah `null` lalu `num <= 100` adalah `true`, jadi tanpa pengecekan kedua perulangan tidak akan berhenti jika pengguna mengeclick CANCEL. Kedua cek dibutuhkan. diff --git a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md index 0788ee76e..38263018a 100644 --- a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md +++ b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md @@ -1,13 +1,13 @@ -importance: 5 +nilai penting: 5 --- -# Repeat until the input is correct +# Ulangi sampai inputnya benar -Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again. +Tulis sebuah perulangan yang meminta angka lebih dari `100`. Jika pendatang memasukan angka lainnya -- tanya mereka untuk menginput lagi. -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. +Perulangan harus bertanya untuk sebuah angka sampai pengunjung memasukan angka lebih dari `100` atau membatalkan input/memasukan sebuah baris kosong. -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. +Disini kita dapat menganggap bahwa pengunjung hanya menginput angka. Tidak perlu menerapkan penanganan spesial untuk input non-numerik di tugas ini. [demo] diff --git a/1-js/02-first-steps/12-while-for/7-list-primes/solution.md b/1-js/02-first-steps/12-while-for/7-list-primes/solution.md index 9ff0663d7..e9483cf22 100644 --- a/1-js/02-first-steps/12-while-for/7-list-primes/solution.md +++ b/1-js/02-first-steps/12-while-for/7-list-primes/solution.md @@ -1,29 +1,29 @@ -There are many algorithms for this task. +Ada banyak algoritma untuk tugas ini. -Let's use a nested loop: +mari gunakan perulangan bersarang: ```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 +Untuk setiap i dalam interval { + cek jika i memiliki pembagi dari 1..i + if yes => nilainya bukan prima + if no => nilainya prima, tampilkan } ``` -The code using a label: +Kode menggunakan label: ```js run let n = 10; nextPrime: -for (let i = 2; i <= n; i++) { // for each i... +for (let i = 2; i <= n; i++) { // untuk setiap 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++) { // mencari pembagi.. + if (i % j == 0) continue nextPrime; // bukan prima, pergi selanjutnya i } - alert( i ); // a prime + alert( i ); // prima } ``` -There's a lot of space to opimize 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. +Ada banyak ruang untuk mengoptimalkannya. Misalnya, kita dapat mencari pembagi dari `2` ke akar kuadrat dari `i`. Tapi bagaimanapun juga, jika kita ingin menjadi sangat efisien untuk interval besar, kita perlu mengubah pendekatan dan mengandalkan pada matematika lanjutan dan algotima kompleks seperti [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) dsb. diff --git a/1-js/02-first-steps/12-while-for/7-list-primes/task.md b/1-js/02-first-steps/12-while-for/7-list-primes/task.md index 6344b9f6f..049174082 100644 --- a/1-js/02-first-steps/12-while-for/7-list-primes/task.md +++ b/1-js/02-first-steps/12-while-for/7-list-primes/task.md @@ -1,17 +1,17 @@ -importance: 3 +nilai penting: 3 --- -# Output prime numbers +# Menghasilkan bilangan prima -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. +Angka integer lebih dari `1` disebut [bilangan prima](https://en.wikipedia.org/wiki/Prime_number) jika itu tidak bisa dibagi tanpa sisa oleh siapapun kecuali `1` dan bilangan itu sendiri. -In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`. +Dengan kata lain, `n > 1` adalah prima jika tidak dapat dibagi secara merata oleh apapun kecuali `1` dan `n`. -For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`. +Contohnya, `5` adalah prima, karna tidak bisa dibagi tanpa sisa oleh `2`, `3` and `4`. -**Write the code which outputs prime numbers in the interval from `2` to `n`.** +**Tulis kode yang menghasilkan bilangan prima dalam interval dari `2` sampai `n`.** -For `n = 10` the result will be `2,3,5,7`. +Untuk `n = 10` hasilnya akan `2,3,5,7`. -P.S. The code should work for any `n`, not be hard-tuned for any fixed value. +P.S. Kode harus bekerja untuk segala `n`, tidak disetel untuk nilai tetap apapun. diff --git a/1-js/02-first-steps/12-while-for/article.md b/1-js/02-first-steps/12-while-for/article.md index c809581f5..9b0d64156 100644 --- a/1-js/02-first-steps/12-while-for/article.md +++ b/1-js/02-first-steps/12-while-for/article.md @@ -1,54 +1,54 @@ -# Loops: while and for +# Perulangan: while dan for -We often need to repeat actions. +Kita sering perlu untuk mengulangi tindakan. -For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. +Contohnya, Mengeluarkan barang dari sebuah daftar satu per satu atau hanya menjalankan kode yang sama untuk setiap nomor dari 1 hingga 10. -*Loops* are a way to repeat the same code multiple times. +*Perulangan* adalah sebuah cara untuk mengulangi kode yang sama beberapa kali. -## The "while" loop +## Perulangan "while" -The `while` loop has the following syntax: +Perulangan `while` memiliki sintaks sebagai berikut: ```js -while (condition) { - // code - // so-called "loop body" +while (kondisi) { + // kode + // disebut "badan perulangan" } ``` -While the `condition` is `true`, the `code` from the loop body is executed. +Ketika `kondisi` bernilai `benar`, `kode` dari badan perulangan tereksekusi. -For instance, the loop below outputs `i` while `i < 3`: +Contohnya, perulangan di bawah mengeluarkan `i` selagi `i < 3`: ```js run let i = 0; -while (i < 3) { // shows 0, then 1, then 2 +while (i < 3) { // menampilkan 0, lalu 1, lalu 2 alert( i ); i++; } ``` -A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations. +Eksekusi tunggal dari badan perulangan disebut *sebuah pengulangan*. Perulangan pada contoh diatas membuat tiga kali pengulangan. -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. +Jika `i++` hilang dari contoh di atas, perulangan akan mengulangi (dalam teori) secara terus-menerus. Pada praktiknya, browser menyediakan cara untuk menghentikan perulangan, dan pada sisi server JavaScript, kita dapat mematikan prosesnya. -Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by `while`. +Ekspresi atau variable apapun bisa menjadi sebuah kondisi perulangan, tidak hanya perbandingan: kondisi terevalusasi dan terkonversi menjadi boolean oleh `while`. -For instance, a shorter way to write `while (i != 0)` is `while (i)`: +Contohnya, cara cepat untuk menulis `while (i != 0)` adalah `while (i)`: ```js run let i = 3; *!* -while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops +while (i) { // ketika i menjadi 0, kondisi bernilai salah, dan perulangan berhenti */!* 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="Kurung kurawal tidak dibutuhkan untuk badan baris tunggal" +Jika badan perulangan mempunyai sebuah pernyataan tunggal, kita dapat menghilangkan kurung kurawal `{…}`: ```js run let i = 3; @@ -58,19 +58,19 @@ while (i) alert(i--); ``` ```` -## The "do..while" loop +## Perulangan "do..while" -The condition check can be moved *below* the loop body using the `do..while` syntax: +Pengecekan kondisi dapat dipindahkan *di bawah* badan perulangan menggunakan `do..while` sintaks: ```js do { - // loop body + // badan perulangan } while (condition); ``` -The loop will first execute the body, then check the condition, and, while it's truthy, execute it again and again. +Perulangan akan mengeksekusi badan terlebih dahulu, lalu memeriksa kondisi, dan, selagi itu bernilai benar, jalankan itu lagi dan lagi. -For example: +Contohnya: ```js run let i = 0; @@ -80,107 +80,106 @@ 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(…) {…}`. +Format penulisan ini hanya digunakan ketika kamu ingin badan dari perulangan tereksekusi **setidaknya sekali** Terlepas dari kondisi menjadi bernilai benar. Biasanya, format lain yang dipilih: `while(…) {…}`. -## The "for" loop +## Perulangan "for" -The `for` loop is the most commonly used loop. +Perulangan `for` adalah perulangan yang paling umum digunakan. -It looks like this: +Itu terlihat seperti ini: ```js -for (begin; condition; step) { - // ... loop body ... +for (awal; kondisi; langkah) { + // ... badan perulangan ... } ``` -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`: +Mari belajar makna dari bagian ini dari contoh. Perulangan dibawah menjalankan `alert(i)` untuk `i` dari `0` sampai dengan (tapi tidak termasuk) `3`: ```js run -for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2 +for (let i = 0; i < 3; i++) { // menampilkan 0, lalu 1, lalu 2 alert(i); } ``` -Let's examine the `for` statement part-by-part: +Mari bahas pernyataan `for` bagian demi bagian: -| part | | | +| bagian | | | |-------|----------|----------------------------------------------------------------------------| -| begin | `i = 0` | Executes once upon entering the loop. | -| condition | `i < 3`| Checked before every loop iteration. If false, the loop stops. | -| step| `i++` | Executes after the body on each iteration but before the condition check. | -| body | `alert(i)`| Runs again and again while the condition is truthy. | +| awal | `i = 0` | jalankan sekali setelah memasuki perulangan. | +| kondisi | `i < 3`| periksa sebelum setiap perulangan. jika salah, perulangan berhenti. | +| langkah| `i++` | Jalankan setelah badan pada setiap perulangan tetapi sebelum kondisi diperiksa. | +| badan | `alert(i)`| Jalankan lagi dan lagi selagi kondisi bernilai benar. | -The general loop algorithm works like this: +Algoritma peulangan umum bekerja seperti ini: ``` -Run begin -→ (if condition → run body and run step) -→ (if condition → run body and run step) -→ (if condition → run body and run step) +jalankan awal +→ (jika kondisi → jalankan badan dan jalankan langkah) +→ (jika kondisi → jalankan badan dan jalankan langkah) +→ (jika kondisi → jalankan badan dan jalankan langkah) → ... ``` -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. +Jika kamu baru pada perulangan, ini dapat membantu untuk kembali ke contoh dan mereproduksi bagamana ini berjalan langkah demi langkah pada sebuah selembar kertas. -Here's exactly what happens in our case: +Inilah yang sebenarnya terjadi pada kasus kita: ```js // for (let i = 0; i < 3; i++) alert(i) -// run begin +// jalankan awal let i = 0 -// if condition → run body and run step +// jika kondisi → jalankan badan dan jalankan langkah if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// jika kondisi → jalankan badan dan jalankan langkah if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// jika kondisi → jalankan badan dan jalankan langkah if (i < 3) { alert(i); i++ } -// ...finish, because now i == 3 +// ...selesai, karena sekarang 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="Deklarasi varibel sebaris" +Disini, "penghitung" variabel `i` dideklarasikan di dalam perulangan. Ini disebut deklarasi variabel "sebaris". variabel ini hanya akan terlihat di dalam perulangan. ```js run for (*!*let*/!* i = 0; i < 3; i++) { alert(i); // 0, 1, 2 } -alert(i); // error, no such variable +alert(i); // error, tidak ada variabel ``` -Instead of defining a variable, we could use an existing one: +Daripada mendefinisikan sebuah variabel, kita dapat menggunakan yang sudah ada: ```js run let i = 0; -for (i = 0; i < 3; i++) { // use an existing variable +for (i = 0; i < 3; i++) { // gunakan variabel yang sudah ada alert(i); // 0, 1, 2 } -alert(i); // 3, visible, because declared outside of the loop +alert(i); // 3, terlihat, karena dideklarasikan diluar dari perulangan ``` ```` -### Skipping parts +### Melewatkan bagian -Any part of `for` can be skipped. +Bagian apapun dari `for` dapat dilewati. -For example, we can omit `begin` if we don't need to do anything at the loop start. +Contoh, kita dapat menghilangkan `awal` jika kita tidak butuh untuk melakukan apapun pada awal perulangan. -Like here: +Seperti ini: ```js run -let i = 0; // we have i already declared and assigned +let i = 0; // kita punya i yang sudah dideklarasikan dan telah ditetapkan -for (; i < 3; i++) { // no need for "begin" +for (; i < 3; i++) { // tidak butuh "awal" alert( i ); // 0, 1, 2 } ``` - -We can also remove the `step` part: +Kita juga bisa menghilangkan bagian `langkah`: ```js run let i = 0; @@ -190,32 +189,31 @@ for (; i < 3;) { } ``` -This makes the loop identical to `while (i < 3)`. +Ini membuat perulangan sama dengan `while (i < 3)`. -We can actually remove everything, creating an infinite loop: +Kita sebenarnya dapat menghilangkan semuanya, membuat sebuah perulangan tak terhingga: ```js for (;;) { - // repeats without limits + // ulangi tanpa batas } ``` +Tolong dicatat bahwa dua `for` titik koma `;` harus ada, jika tidak, akan ada sintaks error. -Please note that the two `for` semicolons `;` must be present. Otherwise, there would be a syntax error. - -## Breaking the loop +## Menghentikan perulangan -Normally, a loop exits when its condition becomes falsy. +Biasanya, sebuah perulangan keluar ketika kondisinya menjadi bernilai salah. -But we can force the exit at any time using the special `break` directive. +Tapi kita dapat memaksanya keluar pada waktu apapun menggunakan perintah spesial `break`. -For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered: +Contohnya, perulangan dibawah menanyakan pengguna untuk serangkaian angka, "hentikan" ketika tidak ada angka yang dimasukan: ```js let sum = 0; while (true) { - let value = +prompt("Enter a number", ''); + let value = +prompt("Masukan sebuah angka", ''); *!* if (!value) break; // (*) @@ -227,32 +225,32 @@ while (true) { alert( 'Sum: ' + 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`. +Perintah `break` teraktivasi pada baris `(*)` jika pengguna memasukan baris kosong atau membatalkan input. itu akan langsung berhenti, melewati kontrol ke baris pertama setelah perulangan. yang bernama, `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. +Kombinasi "perulangan tak terhingga + `break` sesuai kebutuhan" bagus untuk situasi dimana sebuah kondisi perulangan harus diperiksa tidak di awal atau akhir dari perulangan, tapi di pertengahan atau bahkan di beberapa tempat tubuhnya. -## Continue to the next iteration [#continue] +## Lanjutkan ke perulangan berikutnya [#lanjutkan] -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). +Perintah `continue` adalah "versi ringan" dari `break`. Ini tidak mengentikan keseluruhan perulangan. sebagai gantinya, ini menghentikan perulangan saat ini dan memaksa perulangan untuk memulai yang baru (jika kondisi diperbolehkan). -We can use it if we're done with the current iteration and would like to move on to the next one. +Kita dapat menggunakan ini jika kita selesai dengan perulangan saat ini dan ingin pindah ke yang berikutnya. -The loop below uses `continue` to output only odd values: +Perulangan dibawah menggunakan `continue` untuk hanya menampilkan nilai ganjil: ```js run no-beautify for (let i = 0; i < 10; i++) { - // if true, skip the remaining part of the body + // jika benar, lewati bagian badan perulangan yang tersisa *!*if (i % 2 == 0) continue;*/!* alert(i); // 1, then 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. +Untuk nilai genap dari `i`, perintah `continue` mengentikan menjalankan badan dan melewati kontrol ke perulangan `for` berikutnya (dengan nomor berikutnya). jadi `alert` hanya terpanggil untuk nilai ganjil. -````smart header="The `continue` directive helps decrease nesting" -A loop that shows odd values could look like this: +````smart header="Perintah `continue` membantu mengurangi penyarangan" +Sebuah perulangan yang menampilkan nilai ganjil dapat terlihat seperti ini: ```js for (let i = 0; i < 10; i++) { @@ -264,15 +262,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`. +Dari sudut pandang teknis, ini identik dengan contoh diatas. Tentunya, kita dapat membungkus kode dalam sebuah blok `if` daripada menggunakan `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. +Tapi efek sampinya, ini membuat penyarangan satu level lebih (`alert` dipanggil didalam kurung kurawal). Jika kode didalam `if` lebih panjang dari beberapa baris, itu mungkin mengurangi keterbacaan keseluruhan. ```` -````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="Tidak ada `break/continue` ke sisi kanan '?'" +Harap perhatikan bahwa sintaks yang membangun yang bukan ekspresi tidak dapat digunakan dengan operator ternary `?`. Khususnya, perintah seperti `break/continue` tidak diperbolehkan. -For example, if we take this code: +Misalnya, jika kita mengambil kode ini: ```js if (i > 5) { @@ -282,33 +280,33 @@ if (i > 5) { } ``` -...and rewrite it using a question mark: +...dan tulis ulang menggunakan sebuah tanda tanya: ```js no-beautify -(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here +(i > 5) ? alert(i) : *!*continue*/!*; // continue tidak diperbolehkan disini ``` -...it stops working. Code like this will give a syntax error: +...ini berhenti berjalan. Kode seperti ini akan memberi sintaks error: -This is just another reason not to use the question mark operator `?` instead of `if`. +Ini hanya alasan lain untuk tidak menggunakan operator tanda tanya `?` daripada `if`. ```` -## Labels for break/continue +## Label untuk break/continue -Sometimes we need to break out from multiple nested loops at once. +Terkadang kita perlu keluar dari beberapa loop bersarang sekaligus. -For example, in the code below we loop over `i` and `j`, prompting for the coordinates `(i, j)` from `(0,0)` to `(3,3)`: +Contohnya, pada kode dibawah kita lakukan perulangan terhadap `i` and `j`, meminta koordinat `(i, j)` dari `(0,0)` ke`(3,3)`: ```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(`Nilai pada koordinasi (${i},${j})`, ''); - // what if I want to exit from here to Done (below)? + // bagaimana jika saya ingin keluar dari sini ke selesai (dibawah)? } } @@ -316,18 +314,18 @@ for (let i = 0; i < 3; i++) { alert('Done!'); ``` -We need a way to stop the process if the user cancels the input. +Kita butuh cara untuk menghentikan proses jika pengguna membatalkan input. -The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue! +`break` biasa setelah `input` hanya akan menghentikan perulangan dalam. Itu tidak cukup--label, datang untuk menyelamatkan! -A *label* is an identifier with a colon before a loop: +Label adalah sebuah pengidentifikasi dengan sebuah titik dua sebelum perulangan: ```js labelName: for (...) { ... } ``` -The `break ` statement in the loop below breaks out to the label: +Pernyataan `break ` di dalam loop di bawah menghentikan pada label: ```js run no-beautify *!*outer:*/!* for (let i = 0; i < 3; i++) { @@ -336,51 +334,51 @@ The `break ` statement in the loop below breaks out to the label: let input = prompt(`Value at coords (${i},${j})`, ''); - // if an empty string or canceled, then break out of both loops + // jika sebuah string kosong atau terbatalkan, lalu hentikan kedua perulangan if (!input) *!*break outer*/!*; // (*) - // do something with the value... + // lakukan sesuatu dengan nilai... } } alert('Done!'); ``` -In the code above, `break outer` looks upwards for the label named `outer` and breaks out of that loop. +Pada kode diatas, `break outer` melihat keatas untuk label bernama `outer` dan menghentikan perulangan itu. -So the control goes straight from `(*)` to `alert('Done!')`. +Jadi kontrol pergi langsung dari `(*)` ke `alert('Done!')`. -We can also move the label onto a separate line: +Kita juga dapat memindah label ke sebuah baris terpisah: ```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. +Perintah `continue` dapat juga digunakan dengan sebuah label. pada kasus ini, eksekusi kode berpindah ke perulangan label berikutnya. -````warn header="Labels are not a \"goto\"" -Labels do not allow us to jump into an arbitrary place in the code. +````warn header="Label bukan sebuah \"goto\"" +Label tidak mengizinkan kita untuk berpindah tempat sewenang-wenang dalam kode. -For example, it is impossible to do this: +Contohnya, ini mustahil untuk melakukan ini: ```js -break label; // jumps to label? No. +break label; // pindah ke label? Tidak. label: for (...) ``` -A call to `break/continue` is only possible from inside a loop and the label must be somewhere above the directive. +Sebuah panggilan untuk `break/continue` hanya mungkin dari dalam sebuah perulangan dan label harus berada diatas perintah. ```` -## Summary +## Ringkasan -We covered 3 types of loops: +Kita membahas 3 jenis perulangan: -- `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` -- Kondisi diperiksa sebelum setiap perulangan. +- `do..while` -- Kondisi diperiksa setelah setiap perulangan. +- `for (;;)` -- Kondisi diperiksa sebelum setiap perulangan, pengaturan tambahan tersedia. -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. +Untuk membuat sebuah perulangan "tak terhinggaa", biasanya konstruksi `while(true)` digunakan. Demikian sebuah perulangan, seperti yang lainnya, dapat berhenti dengan perintah `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. +Jika kita tidak ingin melakukan apapun di perulangan saat ini dan ingin meneruskan ke yang berikutnya, kita dapat menggunakan perintah `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` mendukung label sebelum perulangan. Label adalah satu-satunya cara untuk `break/continue` menghindari loop bersarang untuk pergi ke luar