Skip to content

Numbers #67

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 7 commits into from
Dec 26, 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
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/1-sum-interface/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ let b = +prompt("The second number?", "");
alert( a + b );
```

Note the unary plus `+` before `prompt`. It immediately converts the value to a number.
Catatan bahwa unary plus `+` sebelum `prompt`. Segera mengkonversi nilai ke angka.

Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`.
Jika tidak, `a` dan `b` akan menjadi string jumlah mereka akan menjadi gabungan mereka, yaitu: `"1" + "2" = "12"`.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/1-sum-interface/task.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
importance: 5
nilai penting: 5

---

# Sum numbers from the visitor
# Jumlahkan angka dari pengunjung

Create a script that prompts the visitor to enter two numbers and then shows their sum.
Buat skrip yang meminta pengunjung untuk memasukkan dua angka dan kemudian menunjukkan jumlah mereka.

[demo]

P.S. There is a gotcha with types.
N.B. Ada gotcha dengan tipe.
16 changes: 8 additions & 8 deletions 1-js/05-data-types/02-number/2-why-rounded-down/solution.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss.
Secara internal pecahan desimal `6.35` adalah sebuah biner tanpa akhir. Seperti biasa dalam kasus seperti ini, disimpan dengan kehilangan presisi.

Let's see:
Ayo lihat:

```js run
alert( 6.35.toFixed(20) ); // 6.34999999999999964473
```

The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down.
Kehilangan presisi dapat menyebabkan peningkatan dan penurunan angka. Dalam kasus khusus ini jumlahnya menjadi sedikit lebih sedikit, itu sebabnya dibulatkan.

And what's for `1.35`?
Dan apa untuk `1.35`?

```js run
alert( 1.35.toFixed(20) ); // 1.35000000000000008882
```

Here the precision loss made the number a little bit greater, so it rounded up.
Di sini kehilangan presisi membuat jumlahnya sedikit lebih besar, jadi itu dibulatkan.

**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
**Bagaimana kita dapat memperbaiki masalah dengan `6.35` jika kita ingin itu dibulatkan dengan cara yang benar?**

We should bring it closer to an integer prior to rounding:
Kita harus membawanya lebih dekat ke integer sebelum pembulatan:

```js run
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000
```

Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it:
Perhatikan bahwa `63.5` tidak memiliki kehilangan presisi sama sekali. Itu karena bagian desimal `0,5` sebenarnya` 1 / 2`. Pecahan yang dibagi oleh kekuatan `2` persis diwakili dalam sistem biner, sekarang kita dapat membulatkannya:


```js run
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/02-number/2-why-rounded-down/task.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
importance: 4
nilai penting: 4

---

# Why 6.35.toFixed(1) == 6.3?
# Kenapa 6.35.toFixed(1) == 6.3?

According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
Berdasarkan dokumentasi `Math.round` dan `toFixed` keduanya membulatkan ke angka terdekat: `0..4` turun sementara `5..9` naik.

For instance:
Contohnya:

```js run
alert( 1.35.toFixed(1) ); // 1.4
```

In the similar example below, why is `6.35` rounded to `6.3`, not `6.4`?
Dalam contoh serupa di bawah ini, mengapa `6.35` dibulatkan menjadi `6.3`, dan tidak `6.4`?

```js run
alert( 6.35.toFixed(1) ); // 6.3
```

How to round `6.35` the right way?
Bagaimana untuk membulatkan `6.35` dengan benar?

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function readNumber() {
alert(`Read: ${readNumber()}`);
```

The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
Solusinya sedikit lebih rumit dari itu karena kita perlu menangani `null`/baris kosong.

So we actually accept the input until it is a "regular number". Both `null` (cancel) and empty line also fit that condition, because in numeric form they are `0`.
Jadi, kita benar-benar menerima input hingga ini merupakan "angka reguler". Baik `null` (cancel) maupun baris kosong juga cocok dengan kondisi itu, karena dalam bentuk numerik mereka adalah` 0`.

After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
Setelah kita berhenti, kita perlu memperlakukan `null` dan khususnya baris kosong (mengembalikan `null`), karena mengonversinya menjadi angka akan mengembalikan `0`.

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/3-repeat-until-number/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
importance: 5
nilai penting: 5

---

# Repeat until the input is a number
# Ulangi sampai masukan adalah sebuah angka

Create a function `readNumber` which prompts for a number until the visitor enters a valid numeric value.
Buatlah sebuah fungsi `readNumber` yang meminta (prompts) nomor hingga pengunjung memasukkan nilai numerik yang valid.

The resulting value must be returned as a number.
Nilai yang dihasilkan harus dikembalikan sebagai angka.

The visitor can also stop the process by entering an empty line or pressing "CANCEL". In that case, the function should return `null`.
Pengunjung juga dapat menghentikan proses dengan memasukkan baris kosong atau menekan "BATAL". Dalam hal ini, fungsi tersebut harus mengembalikan `null`.

[demo]

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/4-endless-loop-error/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
That's because `i` would never equal `10`.
Itu karena `i` tidak akan pernah sebanding dengan `10`.

Run it to see the *real* values of `i`:
Jalankan ini untuk melihat nilai *real* dari `i`:

```js run
let i = 0;
Expand All @@ -10,8 +10,8 @@ while (i < 11) {
}
```

None of them is exactly `10`.
Tidak satu pun dari mereka yang benar-benar `10`.

Such things happen because of the precision losses when adding fractions like `0.2`.
Hal-hal seperti itu terjadi karena kehilangan presisi ketika menambahkan pecahan seperti `0,2`.

Conclusion: evade equality checks when working with decimal fractions.
Kesimpulan: menghindari pemeriksaan kesetaraan saat bekerja dengan pecahan desimal.
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/4-endless-loop-error/task.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 4
nilai penting: 4

---

# An occasional infinite loop
# Lingkaran tak terbatas tak berkala

This loop is infinite. It never ends. Why?
Loop ini tidak terbatas. Tidak pernah berakhir. Mengapa?

```js
let i = 0;
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/8-random-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
We need to "map" all values from the interval 0..1 into values from `min` to `max`.
Kita perlu "memetakan" semua nilai dari interval 0..1 ke dalam nilai dari `min` ke` max`.

That can be done in two stages:
Itu bisa dilakukan dalam dua tahap:

1. If we multiply a random number from 0..1 by `max-min`, then the interval of possible values increases `0..1` to `0..max-min`.
2. Now if we add `min`, the possible interval becomes from `min` to `max`.
1. Jika kita mengalikan angka acak dari 0..1 dengan `max-min`, maka interval nilai yang mungkin meningkat` 0..1` ke `0..max-min`.
2. Sekarang jika kita menambahkan `min`, interval yang mungkin menjadi dari` min` ke `max`.

The function:
Fungsi:

```js run
function random(min, max) {
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/8-random-min-max/task.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
importance: 2
nilai penting: 2

---

# A random number from min to max
# Sebuah angka acak dari min ke max

The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
Fungsi bawaan `Math.random()` membuat sebuah angka acak dari `0` ke `1` (tidak termasuk `1`).

Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
Tulis fungsi `random(min, max)` untuk menghasilkan angka floating-point acak dari `min` ke` max` (tidak termasuk `max`).

Examples of its work:
Contoh kerjanya:

```js
alert( random(1, 5) ); // 1.2345623452
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/02-number/9-random-int-min-max/task.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
importance: 2
nilai penting: 2

---

# A random integer from min to max
# Sebuah integer acak dari min ke max

Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
Buatlah sebuah fungsi `randomInteger(min, max)` yang menghasilkan angka *integer* acak dari `min` ke `max` termasuk keduanya `min` dan `max` sebagai nilai yang mungkin.

Any number from the interval `min..max` must appear with the same probability.
Angka apa pun dari interval `min..max` harus muncul dengan probabilitas yang sama.


Examples of its work:
Contoh kerjanya:

```js
alert( randomInteger(1, 5) ); // 1
alert( randomInteger(1, 5) ); // 3
alert( randomInteger(1, 5) ); // 5
```

You can use the solution of the [previous task](info:task/random-min-max) as the base.
Anda dapat menggunakan solusi dari [tugas sebelumnya](info:task/random-min-max) sebagai basis.
Loading