Skip to content

Commit 6ae7d0d

Browse files
Merge pull request #18 from kooshan75/Data-types
Data types - Numbers
2 parents 8d24702 + dc1aaa2 commit 6ae7d0d

File tree

14 files changed

+258
-282
lines changed

14 files changed

+258
-282
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
2-
31
```js run demo
42
let a = +prompt("The first number?", "");
53
let b = +prompt("The second number?", "");
64

75
alert( a + b );
86
```
97

10-
Note the unary plus `+` before `prompt`. It immediately converts the value to a number.
8+
توجه کنید که جمع واحد `+` قبل از `prompt` است. این یعنی در همان لحظه به مقدار عددی تبدیل می‌شود.
119

12-
Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`.
10+
در غیر اینصورت، `a` و `b`، رشته حرفی میبودند و جمع آنها، پیوست کردن آنها به یکدیگر می‌بود که یعنی:‌ `"1" + "2" = "12"`.

1-js/05-data-types/02-number/1-sum-interface/task.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ importance: 5
22

33
---
44

5-
# Sum numbers from the visitor
5+
# اعداد را جمع کنید
66

7-
Create a script that prompts the visitor to enter two numbers and then shows their sum.
7+
کدی بنویسید که از بازدیدکننده درخواست میکند دو عدد را وارد کند و سپس جمع آنهارا نشان دهد.
88

99
[demo]
1010

11-
P.S. There is a gotcha with types.
11+
پی نوشت: حواستان به مدل‌های داده‌ها باشد.

1-js/05-data-types/02-number/2-why-rounded-down/solution.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss.
2-
3-
Let's see:
1+
در درون سیستم، یک عدد دودویی `6.35` بی‌پایان است. مثل همیشه، در این حالات با یک دقت از دست رفته‌ای ذخیره خواهد شد.
42

53
```js run
64
alert( 6.35.toFixed(20) ); // 6.34999999999999964473
75
```
86

9-
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.
10-
11-
And what's for `1.35`?
7+
این از دست دادن دقت می‌تواند در هم مقدار عدد را افزایش یا کاهش دهد. در این حالت خاص، عدد اندکی کوچک می‌شود، به همین علت به پایین رند می‌شود.
128

9+
و اما `1.35` چی؟
1310
```js run
1411
alert( 1.35.toFixed(20) ); // 1.35000000000000008882
1512
```
1613

17-
Here the precision loss made the number a little bit greater, so it rounded up.
14+
اینجا از دست دادن دقت باعث شد عدد اندکی بزرگتر شود، پس به بالا رند می‌شود.
1815

19-
**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
16+
**چطور میتوانیم مشکل `6.35` را حل کنیم اگر بخواهیم که درست رند شود؟**
2017

21-
We should bring it closer to an integer prior to rounding:
18+
باید به عدد صحیح نزدیک قبلی‌ش رندش کنیم:
2219

2320
```js run
2421
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000
2522
```
2623

27-
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:
28-
24+
توجه داشته باشید که `63.5` هیچ از دست دادن دقتی ندارد. به خاطر قسمت اعشاری آن `0.5` که در اصل `1/2` است. کسرهای تقسیم شده بر دو به درستی در سیستم دودویی نمایش داده می‌شوند، حالا میتوانیم آن را رند کنیم:
2925

3026
```js run
3127
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(rounded) -> 6.4

1-js/05-data-types/02-number/2-why-rounded-down/task.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ importance: 4
22

33
---
44

5-
# Why 6.35.toFixed(1) == 6.3?
5+
# چرا 6.35.toFixed(1) == 6.3?
66

7-
According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
87

9-
For instance:
8+
بنابر مستندات `Math.round` و `toFixed`، هردو به نزدیکترین عدد آن را رند میکنند: `0..4` به پایین رند میشود و `5..9` به بالا.
109

10+
به عنوان مثال:
1111
```js run
1212
alert( 1.35.toFixed(1) ); // 1.4
1313
```
1414

15-
In the similar example below, why is `6.35` rounded to `6.3`, not `6.4`?
16-
15+
در مثال مشابه زیر، چرا `6.35` به `6.3` رند میشود، نه `6.4`؟
1716
```js run
1817
alert( 6.35.toFixed(1) ); // 6.3
1918
```
2019

21-
How to round `6.35` the right way?
22-
20+
چطور `6.35` را رند کنیم؟

1-js/05-data-types/02-number/3-repeat-until-number/solution.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ function readNumber() {
1515
alert(`Read: ${readNumber()}`);
1616
```
1717

18-
The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
18+
جواب اندکی پیچیده‌ست که به خاطر این است که باید `null`/خالی را هم در نظر بگیریم.
1919

20-
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`.
21-
22-
After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
20+
پس ما تا وقتی ورودی را می‌پذیریم که عدد عادی باشد. هردوی `null` (cancel) و خط خالی هم در این شرایط میگنجد، چونکه در فرم عددی صفر هستند.
2321

22+
بعد از اینکه برنامه متوقف شد بایستی `null` و مخصوصا خط‌های خالی (خروجی `null`) را در نظر بگیریم، چون تبدیل آنها به عدد صفر برمی‌گرداند.

1-js/05-data-types/02-number/3-repeat-until-number/task.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ importance: 5
22

33
---
44

5-
# Repeat until the input is a number
5+
# تا زمانی که ورودی یک عدد است تکرار کن
66

7-
Create a function `readNumber` which prompts for a number until the visitor enters a valid numeric value.
7+
یک تابع `readNumber` بسازید که تا زمانی که بازدیدکننده یک عدد وارد نکرده باشد، نرود.
88

9-
The resulting value must be returned as a number.
10-
11-
The visitor can also stop the process by entering an empty line or pressing "CANCEL". In that case, the function should return `null`.
9+
خروجی به فرمت عدد باید باشد.
1210

11+
بازدید کننده میتواند با وارد کردن یک خط خالی یا فشردن دکمه‌ی "CANCEL" پردازش را متوقف کند. در این حالت، تابع خروجی `null` را می‌دهد.
1312
[demo]
1413

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
That's because `i` would never equal `10`.
2-
3-
Run it to see the *real* values of `i`:
1+
به این علت است که `i` هیچوقت برابر ده نمیشود.
42

3+
این تکه کد را اجرا کنید تا مقدار حقیقی `i` را ببینید:
54
```js run
65
let i = 0;
76
while (i < 11) {
@@ -10,8 +9,9 @@ while (i < 11) {
109
}
1110
```
1211

13-
None of them is exactly `10`.
1412

15-
Such things happen because of the precision losses when adding fractions like `0.2`.
13+
هیچکدام از آنها دقیقا ده نیست.
14+
15+
چنین اتفاقاتی به علت از دست رفتن دقت در حین جمع کردن کسرهایی مثل `0.2` رخ می‌دهد.
1616

17-
Conclusion: evade equality checks when working with decimal fractions.
17+
نتیجه: وقتی با کسرهای اعشاری کار میکنید، از تساوی استفاده نکنید.

1-js/05-data-types/02-number/4-endless-loop-error/task.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ importance: 4
22

33
---
44

5-
# An occasional infinite loop
6-
7-
This loop is infinite. It never ends. Why?
5+
# یک حلقه‌ی بی‌نهایت
86

7+
این حلقه بی‌نهایت است. هیچوقت تمام نمی‌شود. چرا؟
98
```js
109
let i = 0;
1110
while (i != 10) {

1-js/05-data-types/02-number/8-random-min-max/solution.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
We need to "map" all values from the interval 0..1 into values from `min` to `max`.
1+
نیاز داریم که تمام مقادیر را در بازه 0..1 به مقادیر از `min` تا `max` ارتباط دهیم.
22

3-
That can be done in two stages:
3+
این به دو روش قابل انجام است:
44

5-
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`.
6-
2. Now if we add `min`, the possible interval becomes from `min` to `max`.
5+
۱. اگر یک عدد تصادفی از 0..1 را در عددی `max-min` ضرب کنیم، بازه‌ی مقادیر ممکن از `0..1` به `0..max-min` افزایش می‌یابد.
76

8-
The function:
7+
۲. حالا اگر `min` را اضافه کنیم، بازه‌ی ممکن از `min` تا `max` میشود.
98

9+
تابع:
1010
```js run
1111
function random(min, max) {
1212
return min + Math.random() * (max - min);

1-js/05-data-types/02-number/8-random-min-max/task.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 2
22

33
---
44

5-
# A random number from min to max
5+
# عددی تصادفی از بین مینیمم تا ماکسیمم
66

7-
The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
7+
تابع از پیش آماده شده‌ی `Math.random()`، یک مقدار تصادفی از `0` تا `1` میسازد (به جز خود یک).
88

9-
Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
9+
تابع `random(min, max)` را بنویسید که یک عدد اعشاری از بین `min` تا`max` را می‌سازد (به جز خود `max`).
1010

11-
Examples of its work:
11+
مثال‌هایی ازینکه چگونه کار می‌کند:
1212

1313
```js
1414
alert( random(1, 5) ); // 1.2345623452

1-js/05-data-types/02-number/9-random-int-min-max/solution.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# The simple but wrong solution
1+
# جواب ساده اما اشتباه
22

3-
The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
3+
جواب اشتباه اما ساده این است که مقداری از `min` تا `max` رو تولید کنیم و رندش کنیم:
44

55
```js run
66
function randomInteger(min, max) {
@@ -11,23 +11,22 @@ function randomInteger(min, max) {
1111
alert( randomInteger(1, 3) );
1212
```
1313

14-
The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
14+
این تابع کار میکند اما غلط است. احتمال اینکه مقادیر لبه `min` و `max` را در نتیجه بگیریم، نصف بقیه‌ست.
1515

16-
If you run the example above many times, you would easily see that `2` appears the most often.
16+
اگر شما مثال بالا را به کرات اجرا کنید، میبینید که عدد ۲ اکثر اوقات ظاهر می‌شود.
1717

18-
That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
18+
به این علت است که `Math.round()` اعداد تصادفی از بازه `1..3` را میگیرد و به شکل زیر رند می‌کند.
1919

2020
```js no-beautify
2121
values from 1 ... to 1.4999999999 become 1
2222
values from 1.5 ... to 2.4999999999 become 2
2323
values from 2.5 ... to 2.9999999999 become 3
2424
```
2525

26-
Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
26+
حالا میتوانیم به وضوح ببینیم که عدد ۲، دو برابر عدد یک مقادیر به آن نسبت داده میشود. همینطور هم برای عدد ۳.
27+
# راه حل صحیح
2728

28-
# The correct solution
29-
30-
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:
29+
راه حال‌های صحیح زیادی وجود دارد. یکی از آنها تنظیم نقاط مرزی‌ست. برای اطمینان یافتن از ازینکه بازه‌ها برابرند میتوانیم مقادیر را از `0.5 تا 3.5` تولید کنیم، سپس احتمال لازم صحیح را به حالات لبه نسبت دهیم:
3130

3231
```js run
3332
*!*
@@ -41,7 +40,7 @@ function randomInteger(min, max) {
4140
alert( randomInteger(1, 3) );
4241
```
4342

44-
An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
43+
راه دیگر میتواند استفاده از `Math.floor` باشد برای یک عدد تصادفی از `min` تا `max+1`:
4544

4645
```js run
4746
*!*
@@ -55,12 +54,12 @@ function randomInteger(min, max) {
5554
alert( randomInteger(1, 3) );
5655
```
5756

58-
Now all intervals are mapped this way:
57+
حال، تمام بازه ها بدین شکل می‌شوند:
5958

6059
```js no-beautify
6160
values from 1 ... to 1.9999999999 become 1
6261
values from 2 ... to 2.9999999999 become 2
6362
values from 3 ... to 3.9999999999 become 3
6463
```
6564

66-
All intervals have the same length, making the final distribution uniform.
65+
همه ی بازه‌ها الان طول یکسانی دارند و در نهایت توزیع یکسانی دارند.

1-js/05-data-types/02-number/9-random-int-min-max/task.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ importance: 2
22

33
---
44

5-
# A random integer from min to max
5+
# عدد صحیح تصادفی از مینیمم تا ماکسیمم
66

7-
Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
7+
یک تابع `randomInteger(min, max)` می‌سازد که یک عدد صحیح تصادفی از `min` تا `max` که هر دو شامل مقادیر `min` و `max` می‌شود را خروجی می‌دهد.
88

9-
Any number from the interval `min..max` must appear with the same probability.
10-
11-
12-
Examples of its work:
9+
هر عدد از بازه‌ی `min..max` باید احتمال یکسانی داشته باشد.
1310

11+
مثال‌هایی از کارکردش:
1412
```js
1513
alert( random(1, 5) ); // 1
1614
alert( random(1, 5) ); // 3
1715
alert( random(1, 5) ); // 5
1816
```
1917

20-
You can use the solution of the [previous task](info:task/random-min-max) as the base.
18+
میتوانید از [راه حل قبلی](info:task/random-min-max) هم به عنوان پایه استفاده کنید.

0 commit comments

Comments
 (0)