Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5739f3a
Translate a part of article
mahdiHash Jun 27, 2021
c1a2361
Translate a part of article
mahdiHash Jun 28, 2021
d1a10f0
Translate a part of article
mahdiHash Jun 29, 2021
4d96a3b
Translate a part of article
mahdiHash Jun 29, 2021
7d98f34
Translate a part of article
mahdiHash Jul 1, 2021
2dff764
Translate a part of article
mahdiHash Jul 2, 2021
584555a
Translate a part of article
mahdiHash Jul 3, 2021
c337877
Translate a part of article
mahdiHash Jul 4, 2021
6bfa153
Translate a part of article
mahdiHash Jul 5, 2021
e185e64
Update article.md
mahdiHash Jul 6, 2021
3e08226
Translate a part of article
mahdiHash Jul 6, 2021
d76e6f6
Translate a part of article
mahdiHash Jul 7, 2021
936258a
Translate a part of article
mahdiHash Jul 8, 2021
d7a9820
Translate a part of article
mahdiHash Jul 10, 2021
a578177
Translate a part of article
mahdiHash Jul 16, 2021
939204b
Change a word for better readability
mahdiHash Jul 16, 2021
d58f616
Translate article
mahdiHash Jul 16, 2021
5db35ba
Remove a line
mahdiHash Jul 16, 2021
d561793
Translate task of "camelcase"
mahdiHash Jul 17, 2021
4633233
Translate task of "average age"
mahdiHash Jul 17, 2021
5efd437
Translate task of "array unique"
mahdiHash Jul 17, 2021
e53a32c
Translate solution of "array unique"
mahdiHash Jul 17, 2021
edd8b08
Translate task of "reduce object"
mahdiHash Jul 17, 2021
cc33e6c
Translate task of "filter range"
mahdiHash Jul 17, 2021
ef1d39d
Translate solution of "filter range"
mahdiHash Jul 17, 2021
a331462
Change a line for better readability
mahdiHash Jul 17, 2021
6fe632d
Translate task of "filter range in-place"
mahdiHash Jul 17, 2021
7f7c58c
Translate solution of "filter range in-place"
mahdiHash Jul 17, 2021
e0200dd
Translate task of "sort back"
mahdiHash Jul 17, 2021
4400d35
Translate task of "copy sort array"
mahdiHash Jul 17, 2021
89fa101
Translate solution of "copy sort array"
mahdiHash Jul 17, 2021
5d26aaf
Translate task of "array get names"
mahdiHash Jul 17, 2021
657d976
Translate task of "calculator extendable"
mahdiHash Jul 17, 2021
a2fe3c6
Translate solution of "calculator extendable"
mahdiHash Jul 17, 2021
089e6d6
Translate task of "map objects"
mahdiHash Jul 17, 2021
e55d5f1
Update task.md
mahdiHash Jul 17, 2021
fc0a407
Translate solution of "map objects"
mahdiHash Jul 17, 2021
5d9184f
Translate task of "sort objects"
mahdiHash Jul 17, 2021
a224a1e
Translate solution of "sort objects"
mahdiHash Jul 17, 2021
416f1df
Translate task of "shuffle"
mahdiHash Jul 17, 2021
c1e60d9
Translate solution of "shuffle"
mahdiHash Jul 17, 2021
9602e1a
Apply suggestions from code review
mahdiHash Jul 17, 2021
4f3ae77
Update 1-js/05-data-types/05-array-methods/9-shuffle/solution.md
mahdiHash Jul 17, 2021
63fb528
Change a line
mahdiHash Jul 17, 2021
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/05-array-methods/1-camelcase/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 5

---

# Translate border-left-width to borderLeftWidth
# عبارت border-left-width را به borderLeftWidth تغییر دهید

Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString".
تابع `camelize(str)` را بنویسید که کلمه‌های جدا شده توسط خط تیره مانند "my-short-string" را به عبارت camel-cased مانند "myShortString" تبدیل می‌کند.

That is: removes all dashes, each word after dash becomes uppercased.
یعنی اینکه تمام خط تیره‌ها را حذف کند و هر کلمه بعد از خط تیره با حرف بزرگ شروع شود.

Examples:
مثال‌ها:

```js
camelize("background-color") == 'backgroundColor';
camelize("list-style-image") == 'listStyleImage';
camelize("-webkit-transition") == 'WebkitTransition';
```

P.S. Hint: use `split` to split the string into an array, transform it and `join` back.
پی‌نوشت راهنمایی: از `split` برای تبدیل رشته به آرایه استفاده کنید، آن را تغییر شکل دهید و با `join` آنها را پیوند بزنید.
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/10-average-age/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Get average age
# دریافت میانگین سن

Write the function `getAverageAge(users)` that gets an array of objects with property `age` and returns the average age.
تابع `getAverageAge(users)` بنویسید که آرایه‌ای از اشیای دارای ویژگی `age` می‌گیرد و میانگین سن را برمی‌گرداند.

The formula for the average is `(age1 + age2 + ... + ageN) / N`.
فرمول میانگین به این صورت است: `(age1 + age2 + ... + ageN) / N`.

For instance:
برای مثال:

```js no-beautify
let john = { name: "John", age: 25 };
Expand Down
22 changes: 11 additions & 11 deletions 1-js/05-data-types/05-array-methods/11-array-unique/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Let's walk the array items:
- For each item we'll check if the resulting array already has that item.
- If it is so, then ignore, otherwise add to results.
بیایید المان‌های آرایه را بررسی کنیم:
- برای هر المان بررسی می‌کنیم که آیا آرایه حاصل دارای آن المان هست یا نه.
- اگر بود، المان را نادیده می‌گیریم، در غیر این صورت آن را به نتایج اضافه می‌کنیم.

```js run
function unique(arr) {
Expand All @@ -22,18 +22,18 @@ let strings = ["Hare", "Krishna", "Hare", "Krishna",
alert( unique(strings) ); // Hare, Krishna, :-O
```

The code works, but there's a potential performance problem in it.
این کد کار می‌کند، اما احتمالا یک اشکال عملکردی دارد.

The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match.
متد `result.includes(str)` درون آرایه `result` را بررسی می‌کند و هر المان را با `str` مقایسه می‌کند تا المان مورد نظر را پیدا کند.

So if there are `100` elements in `result` and no one matches `str`, then it will walk the whole `result` and do exactly `100` comparisons. And if `result` is large, like `10000`, then there would be `10000` comparisons.
ینابراین اگر `100` المان درون `result` وجود داشته باشد و هیچ کدام با `str` برابر نباشد، سپس تمام `result` را بررسی می‌کند و دقیقا `100` مقایسه انجام می‌دهد. و اگر `result` بزرگ باشد، مثلا `10000`، سپس به تعداد `10000` مقایسه وجود خواهد داشت.

That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds.
این به خودی خود مشکل محسوب نمی‌شود، چون موتورهای جاوااسکریپت بسیار سریع هستند، پس بررسی یک آرایه با `10000` المان چند میکروثانیه طول می‌کشد.

But we do such test for each element of `arr`, in the `for` loop.
اما ما در حلقه `for` چنین آزمایشی را برای هر المان درون `arr` انجام می‌دهیم.

So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot.
پس اگر `arr.length` برابر با `10000` باشد ما چیزی مثل `10000*10000` = 100 میلیون مقایسه خواهیم داشت. این مقدار بسیار زیاد است.

So the solution is only good for small arrays.
بنابراین این راه حل تنها برای آرایه‌های کوچک مناسب است.

Further in the chapter <info:map-set> we'll see how to optimize it.
بعدا در فصل <info:map-set> ما یاد می‌گیریم که چگونه آن را بهینه کنیم.
10 changes: 5 additions & 5 deletions 1-js/05-data-types/05-array-methods/11-array-unique/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 4

---

# Filter unique array members
# عضوهای یکتای آرایه را جدا کنید

Let `arr` be an array.
فرض کنیم که `arr` یک آرایه است.

Create a function `unique(arr)` that should return an array with unique items of `arr`.
تابع `unique(arr)` را بنویسید که آرایه‌ای شامل المان‌های یکتای `arr` را برمی‌گرداند.

For instance:
برای مثال:

```js
function unique(arr) {
/* your code */
/* کد شما */
}

let strings = ["Hare", "Krishna", "Hare", "Krishna",
Expand Down
16 changes: 8 additions & 8 deletions 1-js/05-data-types/05-array-methods/12-reduce-object/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Create keyed object from array
# شیءهای کلیددار از آرایه بسازید

Let's say we received an array of users in the form `{id:..., name:..., age... }`.
بیایید فرض کنیم که ما یک آرایه از کاربران به شکل `{id:..., name:..., age:... }` دریافت کرده‌ایم.

Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
یک تابع `groupById(arr)` بسازید که یک شیء از آن ایجاد می‌کند، که `id` به عنوان کلید و المان‌های آرایه به عنوان مقدار موجود هستند.

For example:
برای مثال:

```js
let users = [
Expand All @@ -20,7 +20,7 @@ let users = [
let usersById = groupById(users);

/*
// after the call we should have:
// بعد از فراخوانی ما باید این را داشته باشیم:

usersById = {
john: {id: 'john', name: "John Smith", age: 20},
Expand All @@ -30,8 +30,8 @@ usersById = {
*/
```

Such function is really handy when working with server data.
چنین تابعی هنگام کار کردن با داده سرور خیلی به کار می‌آید.

In this task we assume that `id` is unique. There may be no two array items with the same `id`.
در این تکلیف ما فرض می‌کنیم که `id` یکتا است. هیچ دو المانی از آرایه وجود ندارد که `id` یکسانی داشته باشند.

Please use array `.reduce` method in the solution.
لطفا از متد `.reduce` در راه حل استفاده کنید.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
``` js run
function filterRange(arr, a, b) {
// added brackets around the expression for better readability
// برای خوانایی بهتر به دور عبارت، پرانتز اضافه کردیم
return arr.filter(item => (a <= item && item <= b));
}

let arr = [5, 3, 8, 1];

let filtered = filterRange(arr, 1, 4);

alert( filtered ); // 3,1 (matching values)
alert( filtered ); // 3,1 (مقدارهای مورد نظر)

alert( arr ); // 5,3,8,1 (not modified)
alert( arr ); // 5,3,8,1 (تغییر داده نشده)
```
12 changes: 6 additions & 6 deletions 1-js/05-data-types/05-array-methods/2-filter-range/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ importance: 4

---

# Filter range
# جداسازی توسط حدود

Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements with values higher or equal to `a` and lower or equal to `b` and return a result as an array.
یک تابع `filterRange(arr, a, b)` بنویسید که یک آرایه `arr` دریافت می‌کند، به دنبال المان‌های بزرگتر یا مساوی `a` و کوچکتر یا مساوی `b` می‌گردد و نتیجه را به شکل آرایه برمی‌گرداند.

The function should not modify the array. It should return the new array.
تابع نباید آرایه داده شده را تغییر دهد. باید آرایه جدید را برگرداند.

For instance:
برای مثال:

```js
let arr = [5, 3, 8, 1];

let filtered = filterRange(arr, 1, 4);

alert( filtered ); // 3,1 (matching values)
alert( filtered ); // 3,1 (مقدارهای مورد نظر)

alert( arr ); // 5,3,8,1 (not modified)
alert( arr ); // 5,3,8,1 (تغییر داده نشده)
```

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function filterRangeInPlace(arr, a, b) {
for (let i = 0; i < arr.length; i++) {
let val = arr[i];

// remove if outside of the interval
// اگر بیرون از بازه بود آن را حذف کن
if (val < a || val > b) {
arr.splice(i, 1);
i--;
Expand All @@ -15,7 +15,7 @@ function filterRangeInPlace(arr, a, b) {

let arr = [5, 3, 8, 1];

filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
filterRangeInPlace(arr, 1, 4); // تمام اعداد به جز 1 تا 4 را حذف کرد

alert( arr ); // [3, 1]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 4

---

# Filter range "in place"
# جداسازی توسط حدود «در محل»

Write a function `filterRangeInPlace(arr, a, b)` that gets an array `arr` and removes from it all values except those that are between `a` and `b`. The test is: `a ≤ arr[i] ≤ b`.
تابع `filterRangeInPlace(arr, a, b)` را بنویسید که یک آرایه `arr` دریافت می‌کند و تمام مقدارها به جز آنهایی که بین `a` و `b` هستند را حذف می‌کند. آزمایش به این صورت است: `a ≤ arr[i] ≤ b`.

The function should only modify the array. It should not return anything.
تابع باید فقط آرایه را تغییر دهد. نباید چیزی را برگرداند.

For instance:
برای مثال:
```js
let arr = [5, 3, 8, 1];

filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
filterRangeInPlace(arr, 1, 4); // تمام اعداد به جز 1 تا 4 را حذف کردیم

alert( arr ); // [3, 1]
```
4 changes: 2 additions & 2 deletions 1-js/05-data-types/05-array-methods/4-sort-back/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 4

---

# Sort in decreasing order
# مرتب‌سازی با ترتیب نزولی

```js
let arr = [5, 2, 1, -10, 8];

// ... your code to sort it in decreasing order
// ... کد شما برای مرتب کردن آرایه با ترتیب نزولی

alert( arr ); // 8, 5, 2, 1, -10
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
We can use `slice()` to make a copy and run the sort on it:
برای ایجاد یک کپی و اجرای مرتب‌سازی روی آن، می‌توانیم از `slice()` استفاده کنیم:

```js run
function copySorted(arr) {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Copy and sort array
# کپی و مرتب کردن آرایه

We have an array of strings `arr`. We'd like to have a sorted copy of it, but keep `arr` unmodified.
ما آرایه‌ای از رشته‌ها `arr` داریم. ما می‌خواهیم یک کپی مرتب شده از آن را داشته باشیم اما `arr` را تغییر ندهیم.

Create a function `copySorted(arr)` that returns such a copy.
یک تابع `copySorted(arr)` بسازید که چنین کپی‌ای را برگرداند.

```js
let arr = ["HTML", "JavaScript", "CSS"];

let sorted = copySorted(arr);

alert( sorted ); // CSS, HTML, JavaScript
alert( arr ); // HTML, JavaScript, CSS (no changes)
alert( arr ); // HTML, JavaScript, CSS (بدون تغییر)
```
8 changes: 4 additions & 4 deletions 1-js/05-data-types/05-array-methods/6-array-get-names/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Map to names
# اسم‌ها را دریابید

You have an array of `user` objects, each one has `user.name`. Write the code that converts it into an array of names.
شما یک آرایه از شیءهای `user` دارید که هر کدام دارای `user.name` هستند. کدی بنویسید که آن را به آرایه‌ای از اسم‌ها تبدیل می‌کند.

For instance:
برای مثال:

```js no-beautify
let john = { name: "John", age: 25 };
Expand All @@ -15,7 +15,7 @@ let mary = { name: "Mary", age: 28 };

let users = [ john, pete, mary ];

let names = /* ... your code */
let names = /* ... کد شما */

alert( names ); // John, Pete, Mary
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

- Please note how methods are stored. They are simply added to `this.methods` property.
- All tests and numeric conversions are done in the `calculate` method. In future it may be extended to support more complex expressions.
- لطفا در نظر داشته باشید که متدها چگونه ذخیره شده‌اند. آنها به سادگی به ویژگی `this.methods` اضافه شده‌اند.
- تمام آزمایش‌ها و تبدیلات عددی در متد `calculate` انجام می‌شوند. در آینده ممکن است برای پشتیبانی از عبارات پیچیده‌تر توسعه بیابد.
20 changes: 10 additions & 10 deletions 1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ importance: 5

---

# Create an extendable calculator
# یک ماشین حساب قابل توسعه بسازید

Create a constructor function `Calculator` that creates "extendable" calculator objects.
یک تابع سازنده `Calculator` بسازید که شیءهای ماشین حساب «قابل توسعه» می‌سازد.

The task consists of two parts.
این تکلیف از دو بخش تشکیل شده است.

1. First, implement the method `calculate(str)` that takes a string like `"1 + 2"` in the format "NUMBER operator NUMBER" (space-delimited) and returns the result. Should understand plus `+` and minus `-`.
1. اول، متد `calculate(str)` را بسازید که یک رشته مانند `"1 + 2"` را در شکل «عدد عملگر عدد» دریافت می‌کند و نتیجه را برمی‌گرداند. این متد باید جمع `+` و منها `-` را متوجه شود.

Usage example:
مثالی از کاربرد آن:

```js
let calc = new Calculator;

alert( calc.calculate("3 + 7") ); // 10
```
2. Then add the method `addMethod(name, func)` that teaches the calculator a new operation. It takes the operator `name` and the two-argument function `func(a,b)` that implements it.
2. سپس متد `addMethod(name, func)` را اضافه کنید که به ماشین حساب یک عملیات جدید را آموزش می‌دهد. این متد اسم عملگر `name` و تابع دو آرگومانی `func(a,b)` که عملیات را پیاده‌سازی می‌کند را دریافت می‌کند.

For instance, let's add the multiplication `*`, division `/` and power `**`:
برای مثال، بیایید عمل ضرب `*`، تقسیم `/` و به توان رساندن `**` را اضافه کنیم:

```js
let powerCalc = new Calculator;
Expand All @@ -31,6 +31,6 @@ The task consists of two parts.
alert( result ); // 8
```

- No parentheses or complex expressions in this task.
- The numbers and the operator are delimited with exactly one space.
- There may be error handling if you'd like to add it.
- پرانتز یا عبارات پیچیده در این تکلیف وجود ندارند.
- اعداد و عملگر دقیقا به یک فاصله خالی محدود می‌شوند.
- اگر دوست داشته باشید می‌توانید مدیریت ارور را هم اضافه کنید.
10 changes: 5 additions & 5 deletions 1-js/05-data-types/05-array-methods/7-map-objects/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ alert( usersMapped[0].id ); // 1
alert( usersMapped[0].fullName ); // John Smith
```

Please note that in the arrow functions we need to use additional brackets.
لطفا در نظر داشته باشید که در تابع‌های کمانی ما باید از پرانتزهای اضافی استفاده کنیم.

We can't write like this:
نمی‌توانیم اینگونه بنویسیم:
```js
let usersMapped = users.map(user => *!*{*/!*
fullName: `${user.name} ${user.surname}`,
id: user.id
});
```

As we remember, there are two arrow functions: without body `value => expr` and with body `value => {...}`.
همانطور که به یاد داریم، دو نوع تابع کمانی وجود دارد: بدون بدنه `value => expr` و همراه با بدنه `value => {...}`.

Here JavaScript would treat `{` as the start of function body, not the start of the object. The workaround is to wrap them in the "normal" brackets:
اینجا جاوااسکریپت با `}` به عنوان آغاز بدنه تابع رفتار می‌کند نه آغاز شیء. راه حل در پیچیدن آنها درون یک «پرانتر» است:

```js
let usersMapped = users.map(user => *!*({*/!*
Expand All @@ -46,6 +46,6 @@ let usersMapped = users.map(user => *!*({*/!*
}));
```

Now fine.
حالا درست است.


Loading