Skip to content
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

[과제번역] Part 1. 5.2 숫자형 #1171 #1172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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)의 답안을 토대로 사용해도 됩니다.