Skip to content

Loops: while and for #30

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 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Jawabanya: `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`.
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
```
6 changes: 3 additions & 3 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
@@ -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;
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.
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.
12 changes: 6 additions & 6 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
@@ -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;
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.**
**Jawabanya: dari `0` ke `4` di kedua kasus.**

```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`:
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`.
12 changes: 6 additions & 6 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
@@ -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 );
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.
Kita menggunakan operator "modulo" `%` untuk mendapatkan sisanya dan untuk mengecek kegenapan disini.
6 changes: 3 additions & 3 deletions 1-js/02-first-steps/12-while-for/4-for-even/task.md
Original file line number Diff line number Diff line change
@@ -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]
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
@@ -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++) {
Expand Down
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("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.
10 changes: 5 additions & 5 deletions 1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md
Original file line number Diff line number Diff line change
@@ -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]
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.
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.
16 changes: 8 additions & 8 deletions 1-js/02-first-steps/12-while-for/7-list-primes/task.md
Original file line number Diff line number Diff line change
@@ -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.
Loading