Skip to content
Open
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
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`.
`i`λŠ” μ ˆλŒ€ `10`이 될 수 μ—†κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

Run it to see the *real* values of `i`:
μ‹€ν–‰ν•˜μ—¬ `i`의 *μ‹€μ œ* 값을 ν™•μΈν•΄λ΄…μ‹œλ‹€:

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

None of them is exactly `10`.
μ–΄λ–€ 것도 μ •ν™•νžˆ `10`이 λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Such things happen because of the precision losses when adding fractions like `0.2`.
`0.2`와 같은 μ†Œμˆ˜λ₯Ό λ”ν•˜λ©΄ 정밀도가 λ–¨μ–΄μ§€κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

Conclusion: evade equality checks when working with decimal fractions.
κ²°λ‘ : μ†Œμˆ˜λ₯Ό λ‹€λ£° λ•ŒλŠ” 동등 μ—°μ‚°μž 비ꡐλ₯Ό μ§€μ–‘ν•©μ‹œλ‹€.
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/4-endless-loop-error/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 4

---

# An occasional infinite loop
# 간헐적 λ¬΄ν•œλ£¨ν”„

This loop is infinite. It never ends. Why?
이것은 λ¬΄ν•œλ£¨ν”„μž…λ‹ˆλ‹€. λλ‚˜μ§ˆ μ•Šμ£ . μ™œ κ·ΈλŸ΄κΉŒμš”?

```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`.
λ¨Όμ € 0..1 κ΅¬κ°„μ˜ λͺ¨λ“  값을 `min`κ³Ό `max`의 사이가 될 수 μžˆλ„λ‘ λ§€ν•‘ν•΄μ•Ό ν•©λ‹ˆλ‹€.

That can be done in two stages:
두 λ‹¨κ³„λ‘œ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

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. 0..1 μ‚¬μ΄μ˜ λ¬΄μž‘μœ„ 숫자λ₯Ό `max-min`으둜 κ³±ν•œλ‹€λ©΄, κ°€λŠ₯ν•œ κ°’μ˜ ꡬ간은 `0..1`μ—μ„œ `0..max-min`이 λ©λ‹ˆλ‹€.
2. 이제 `min`을 더해주면, κ°€λŠ₯ν•œ ꡬ간은 `min` 이상 `max` 미만이 λ©λ‹ˆλ‹€.

The function:
λ‹΅μ•ˆ:

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

---

# A random number from min to max
# min 이상 max 미만 λ¬΄μž‘μœ„ 숫자

The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
λ‚΄μž₯ ν•¨μˆ˜ `Math.random()`은 `0` 이상 `1` 미만의 λ¬΄μž‘μœ„ 값을 μƒμ„±ν•©λ‹ˆλ‹€.

Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
`min` 이상 `max` 미만의 λ¬΄μž‘μœ„ λΆ€λ™μ†Œμˆ˜μ  숫자λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜ `random(min, max)`을 μž‘μ„±ν•΄ λ³΄μ„Έμš”.

Examples of its work:
μ˜ˆμ‹œ:

```js
alert( random(1, 5) ); // 1.2345623452
Expand Down
38 changes: 19 additions & 19 deletions 1-js/05-data-types/02-number/9-random-int-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The simple but wrong solution
# κ°„λ‹¨ν•˜μ§€λ§Œ ν‹€λ¦° λ‹΅μ•ˆ

The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
κ°€μž₯ κ°„λ‹¨ν•˜μ§€λ§Œ 잘λͺ»λœ λ‹΅μ•ˆμ€ `min`λΆ€ν„° `max`κΉŒμ§€ 값을 μƒμ„±ν•˜κ³  λ°˜μ˜¬λ¦Όν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

```js run
function randomInteger(min, max) {
Expand All @@ -11,28 +11,28 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
κΈ°λŠ₯은 λ™μž‘ν•˜λ‚˜ λΆ€μ •ν™•ν•©λ‹ˆλ‹€. λͺ¨μ„œλ¦Ώκ°’인 `min`κ³Ό `max`κ°€ λ‚˜μ˜¬ ν™•λ₯ μ΄ λ‹€λ₯Έ 것보닀 두 λ°° μ μŠ΅λ‹ˆλ‹€.

If you run the example above many times, you would easily see that `2` appears the most often.
μœ„ 예제λ₯Ό μ—¬λŸ¬ 번 μ‹€ν–‰ν•œλ‹€λ©΄ `2`κ°€ κ°€μž₯ 많이 λ‚˜νƒ€λ‚œλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
μ΄λŠ” `Math.round()`κ°€ `1..3` κ΅¬κ°„μ˜ λ¬΄μž‘μœ„ 숫자λ₯Ό λ‹€μŒκ³Ό 같이 λ°˜μ˜¬λ¦Όν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

```js no-beautify
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
1 λΆ€ν„° ... 1.4999999999 κΉŒμ§€μ˜ 값은 1 이 λ©λ‹ˆλ‹€.
1.5 λΆ€ν„° ... 2.4999999999 κΉŒμ§€μ˜ 값은 2 이 λ©λ‹ˆλ‹€.
2.5 λΆ€ν„° ... 2.9999999999 κΉŒμ§€μ˜ 값은 3 이 λ©λ‹ˆλ‹€.
```

Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
`1`이 `2`보닀 두 λ°° 적게 λ‚˜μ˜€λŠ” 것을 λ³Ό 수 있죠. `3`도 λ§ˆμ°¬κ°€μ§€μž…λ‹ˆλ‹€.

# The correct solution
# μ˜¬λ°”λ₯Έ λ‹΅μ•ˆ

There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
μ—¬λŸ¬ λ‹΅μ•ˆμ΄ μžˆμ§€λ§Œ 그쀑 ν•˜λ‚˜λŠ” ꡬ간 경계λ₯Ό μ‘°μ •ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. λ™μΌν•œ ꡬ간을 보μž₯ν•˜κΈ° μœ„ν•΄ `0.5 μ—μ„œ 3.5` κ΅¬κ°„μ˜ 값을 μƒμ„±ν•˜μ—¬ λͺ¨μ„œλ¦Ώκ°’에 ν•„μš”ν•œ ν™•λ₯ μ„ 더할 수 μžˆμŠ΅λ‹ˆλ‹€.

```js run
*!*
function randomInteger(min, max) {
// now rand is from (min-0.5) to (max+0.5)
// rand λŠ” (min-0.5) λΆ€ν„° (max+0.5) κΉŒμ§€μž…λ‹ˆλ‹€.
let rand = min - 0.5 + Math.random() * (max - min + 1);
return Math.round(rand);
}
Expand All @@ -41,12 +41,12 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
λ‹€λ₯Έ λ°©λ²•μœΌλ‘œλŠ” `Math.floor`λ₯Ό μ΄μš©ν•˜μ—¬ `min`λΆ€ν„° `max+1`κΉŒμ§€μ˜ λ¬΄μž‘μœ„ 숫자λ₯Ό λ§Œλ“œλŠ” κ²ƒμž…λ‹ˆλ‹€.

```js run
*!*
function randomInteger(min, max) {
// here rand is from min to (max+1)
// μ—¬κΈ°μ„œ rand λŠ” min λΆ€ν„° (max+1) κΉŒμ§€μž…λ‹ˆλ‹€.
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
Expand All @@ -55,12 +55,12 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

Now all intervals are mapped this way:
λͺ¨λ“  ꡬ간이 λ‹€μŒκ³Ό 같이 λ§€ν•‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€:

```js no-beautify
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
1 λΆ€ν„° ... 1.9999999999 κΉŒμ§€μ˜ 값은 1 이 λ©λ‹ˆλ‹€.
2 λΆ€ν„° ... 2.9999999999 κΉŒμ§€μ˜ 값은 2 κ°€ λ©λ‹ˆλ‹€.
3 λΆ€ν„° ... 3.9999999999 κΉŒμ§€μ˜ 값은 3 이 λ©λ‹ˆλ‹€.
```

All intervals have the same length, making the final distribution uniform.
λͺ¨λ“  κ΅¬κ°„μ˜ 길이가 λ™μΌν•΄μ‘Œκ³ , μ΅œμ’… 뢄포도 κ· μΌν•΄μ‘ŒμŠ΅λ‹ˆλ‹€.
11 changes: 6 additions & 5 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
Expand Up @@ -2,19 +2,20 @@ importance: 2

---

# A random integer from min to max
# min 이상 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.
`min` 이상 `max` μ΄ν•˜μ˜ λ¬΄μž‘μœ„ *μ •μˆ˜*λ₯Ό μƒμ„±ν•˜λŠ” ν•¨μˆ˜ `randomInteger(min, max)`λ₯Ό μž‘μ„±ν•΄ λ³΄μ„Έμš”.

Any number from the interval `min..max` must appear with the same probability.

`min..max` κ΅¬κ°„μ˜ μ–΄λŠ μˆ«μžλΌλ„ 같은 ν™•λ₯ λ‘œ λ‚˜νƒ€λ‚˜μ•Ό ν•©λ‹ˆλ‹€.

Examples of its work:

μ˜ˆμ‹œ:

```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.
[μœ„ 문제](info:task/random-min-max)의 λ‹΅μ•ˆμ„ ν† λŒ€λ‘œ μ‚¬μš©ν•΄λ„ λ©λ‹ˆλ‹€.