Skip to content

Commit f6ff96a

Browse files
Array Methods Tasks
1 parent 5c355cd commit f6ff96a

16 files changed

Lines changed: 118 additions & 115 deletions

File tree

1-js/05-data-types/05-array-methods/1-camelcase/task.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ importance: 5
22

33
---
44

5-
# Translate border-left-width to borderLeftWidth
5+
# Tحوّل «border-left-width» إلى «borderLeftWidth»
66

7-
Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString".
7+
اكتب دالة camelize(str)‎ تغيّر الكلمات المقسومة بِشَرطات مثل «my-short-string» إلى عبارات بتنسيق «سنام الجمل»: «myShortString».
88

9-
That is: removes all dashes, each word after dash becomes uppercased.
9+
بعبارة أخرى: أزِل كلّ الشرطات وحوّل أوّل حرف من كلّ كلمة بعدها إلى الحالة الكبيرة.
1010

11-
Examples:
11+
أمثلة:
12+
13+
:
1214

1315
```js
14-
camelize("background-color") == 'backgroundColor';
15-
camelize("list-style-image") == 'listStyleImage';
16-
camelize("-webkit-transition") == 'WebkitTransition';
16+
camelize("background-color") == "backgroundColor";
17+
camelize("list-style-image") == "listStyleImage";
18+
camelize("-webkit-transition") == "WebkitTransition";
1719
```
1820

19-
P.S. Hint: use `split` to split the string into an array, transform it and `join` back.
21+
تلميح: استعمل split لتقسيم السلسلة النصية إلى مصفوفة، ثمّ عدّل عناصرها وأعِد ربطها بتابِع join.

1-js/05-data-types/05-array-methods/10-average-age/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ importance: 4
22

33
---
44

5-
# Get average age
5+
# ما متوسّط الأعمار؟
66

7-
Write the function `getAverageAge(users)` that gets an array of objects with property `age` and returns the average age.
7+
اكتب دالة getAverageAge(users)‎ تأخذ مصفوفة من كائنات لها الصفة age وتُعيد متوسّط الأعمار.
88

9-
The formula for the average is `(age1 + age2 + ... + ageN) / N`.
9+
معادلة المتوسّط: ‎(age1 + age2 + ... + ageN) / N.
1010

11-
For instance:
11+
مثال:
1212

1313
```js no-beautify
1414
let john = { name: "John", age: 25 };
1515
let pete = { name: "Pete", age: 30 };
1616
let mary = { name: "Mary", age: 29 };
1717

18-
let arr = [ john, pete, mary ];
18+
let arr = [john, pete, mary];
1919

20-
alert( getAverageAge(arr) ); // (25 + 30 + 29) / 3 = 28
20+
alert(getAverageAge(arr)); // (25 + 30 + 29) / 3 = 28
2121
```
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
Let's walk the array items:
2-
- For each item we'll check if the resulting array already has that item.
3-
- If it is so, then ignore, otherwise add to results.
1+
ما سنفعل هو المرور على عناصر المصفوفة:
2+
3+
- سنفحص كلّ عنصر ونرى إن كان في المصفوفة الناتجة.
4+
- إن كان كذلك… نُهمله، وإن لم يكن، نُضيفه إلى المصفوفة.
45

56
```js run demo
67
function unique(arr) {
@@ -15,25 +16,23 @@ function unique(arr) {
1516
return result;
1617
}
1718

18-
let strings = ["Hare", "Krishna", "Hare", "Krishna",
19-
"Krishna", "Krishna", "Hare", "Hare", ":-O"
19+
let strings = [
20+
"Hare",
21+
"Krishna",
22+
"Hare",
23+
"Krishna",
24+
"Krishna",
25+
"Krishna",
26+
"Hare",
27+
"Hare",
28+
":-O"
2029
];
2130

22-
alert( unique(strings) ); // Hare, Krishna, :-O
31+
alert(unique(strings)); // Hare, Krishna, :-O
2332
```
2433

25-
The code works, but there's a potential performance problem in it.
26-
27-
The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match.
28-
29-
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.
30-
31-
That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds.
32-
33-
But we do such test for each element of `arr`, in the `for` loop.
34-
35-
So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot.
34+
صحيح أنّ الكود يعمل، إلّا أنّ فيه مشكلة أداء محتملة. خلف الكواليس، يمرّ التابِع result.includes(str)‎ على المصفوفة result ويقارن كلّ عنصر مع str ليجد المطابقة المنشودة. لذا لو كان في result مئة 100 عنصر وما من أيّ مطابقة مع str، فعليها المرور على جُلّ result وإجراء 100 حالة مقارنة كاملة. ولو كانت result كبيرة مثل 10000 فيعني ذلك 10000 حالة مقارنة.
3635

37-
So the solution is only good for small arrays.
36+
إلى هنا لا مشكلة، لأنّ محرّكات جافاسكربت سريعة جدًا، والمرور على 1000 عنصر في المصفوفة يحدث في بضعة ميكروثوان. ولكنّا هنا في حلقة for نُجري هذه الشروط لكلّ عنصر من arr. فإن كانت arr.length تساوي 10000 فيعني أنّا سنُجري 10000\*10000 = مئة مليون حالة مقارنة. كثير جدًا.
3837

39-
Further in the chapter <info:map-set> we'll see how to optimize it.
38+
إذًا، فهذا الحل ينفع للمصفوفات الصغيرة فقط. سنرى لاحقًا في الفصل كيف نحسّن هذا الكود

1-js/05-data-types/05-array-methods/11-array-unique/task.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ importance: 4
22

33
---
44

5-
# Filter unique array members
5+
# ترشيح العناصر الفريدة في المصفوفة
66

7-
Let `arr` be an array.
7+
لمّا أنّ arr مصفوفة، أنشِئ دالة unique(arr)‎ تُعيد مصفوفة فيها عناصر arr غير مكرّرة.
88

9-
Create a function `unique(arr)` that should return an array with unique items of `arr`.
10-
11-
For instance:
9+
مثال:
1210

1311
```js
1412
function unique(arr) {
1513
/* your code */
1614
}
1715

18-
let strings = ["Hare", "Krishna", "Hare", "Krishna",
19-
"Krishna", "Krishna", "Hare", "Hare", ":-O"
16+
let strings = [
17+
"Hare",
18+
"Krishna",
19+
"Hare",
20+
"Krishna",
21+
"Krishna",
22+
"Krishna",
23+
"Hare",
24+
"Hare",
25+
":-O"
2026
];
2127

22-
alert( unique(strings) ); // Hare, Krishna, :-O
28+
alert(unique(strings)); // Hare, Krishna, :-O
2329
```

1-js/05-data-types/05-array-methods/12-reduce-object/task.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ importance: 4
22

33
---
44

5-
# Create keyed object from array
5+
# انشاء مفاتيح خاصة بكائنات المصفوفة
66

7-
Let's say we received an array of users in the form `{id:..., name:..., age... }`.
7+
دعنا نقول أننا نستقبل مصفوفة خاصة بالمستخدمين داخل form مكونة `{id:..., name:..., age... }`
88

9-
Create a function `groupById(arr)` that creates an object from it, with `id` as the key, and array items as values.
9+
اكتب دالة `groupById(arr)` لانشاء كائن منها يحتوى على `id` كمفتاح و عناصر المصفوفة كقيم
1010

11-
For example:
11+
مثال :
1212

1313
```js
1414
let users = [
15-
{id: 'john', name: "John Smith", age: 20},
16-
{id: 'ann', name: "Ann Smith", age: 24},
17-
{id: 'pete', name: "Pete Peterson", age: 31},
15+
{ id: "john", name: "John Smith", age: 20 },
16+
{ id: "ann", name: "Ann Smith", age: 24 },
17+
{ id: "pete", name: "Pete Peterson", age: 31 }
1818
];
1919

2020
let usersById = groupById(users);
@@ -30,8 +30,8 @@ usersById = {
3030
*/
3131
```
3232

33-
Such function is really handy when working with server data.
33+
هذه الوظيفة مفيدة حقًا عند العمل مع بيانات الخادم.
3434

35-
In this task we assume that `id` is unique. There may be no two array items with the same `id`.
35+
في هذه المهمة نفترض أن `id` فريد. قد لا يكون هناك عنصران للصفيف بنفس "المعرف".
3636

37-
Please use array `.reduce` method in the solution.
37+
يُرجى استخدام طريقة الصفيف `.reduce` في الحل.

1-js/05-data-types/05-array-methods/2-filter-range/task.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ importance: 4
22

33
---
44

5-
# Filter range
5+
# نطاق ترشيح
66

7-
Write a function `filterRange(arr, a, b)` that gets an array `arr`, looks for elements between `a` and `b` in it and returns an array of them.
7+
اكتب دالة filterRange(arr, a, b)‎ تأخذ المصفوفة arr، وتبحث في عناصرها بين a وb وتُعيد مصفوفة بها. يجب ألّا تُعدّل الدالة المصفوفة، بل إعادة مصفوفة جديدة.
88

9-
The function should not modify the array. It should return the new array.
10-
11-
For instance:
9+
مثال:
1210

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

16-
let filtered = filterRange(arr, 1, 4);
14+
let filtered = filterRange(arr, 1, 4);
1715

18-
alert( filtered ); // 3,1 (matching values)
16+
alert(filtered); // 3,1 (matching values)
1917

20-
alert( arr ); // 5,3,8,1 (not modified)
18+
alert(arr); // 5,3,8,1 (not modified)
2119
```
22-

1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ importance: 4
22

33
---
44

5-
# Filter range "in place"
5+
# نطاق ترشيح «كما هو»
66

7-
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`.
7+
اكتب دالة filterRangeInPlace(arr, a, b)‎ تأخذ المصفوفة arr وتُزيل منها كل القيم عدا تلك بين a وb. الشرط هو: ‎a ≤ arr ≤ b.
88

9-
The function should only modify the array. It should not return anything.
9+
يجب أن تُعدّل الدالة المصفوفة، ولا تُعيد شيئًا.
10+
11+
مثال:
1012

11-
For instance:
1213
```js
1314
let arr = [5, 3, 8, 1];
1415

1516
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
1617

17-
alert( arr ); // [3, 1]
18+
alert(arr); // [3, 1]
1819
```

1-js/05-data-types/05-array-methods/4-sort-back/task.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ importance: 4
22

33
---
44

5-
# Sort in decreasing order
5+
# الفرز بالترتيب التنازلي
66

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

1010
// ... your code to sort it in decreasing order
1111

12-
alert( arr ); // 8, 5, 2, 1, -10
12+
alert(arr); // 8, 5, 2, 1, -10
1313
```
14-

1-js/05-data-types/05-array-methods/5-copy-sort-array/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Copy and sort array
5+
# نسخ المصفوفة وفرزها
66

7-
We have an array of strings `arr`. We'd like to have a sorted copy of it, but keep `arr` unmodified.
7+
في يدنا مصفوفة من السلاسل النصية arr. نريد نسخة مرتّبة عنها وترك arr بلا تعديل.
88

9-
Create a function `copySorted(arr)` that returns such a copy.
9+
أنشِئ دالة copySorted(arr)‎ تُعيد هذه النسخة.
1010

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

1414
let sorted = copySorted(arr);
1515

16-
alert( sorted ); // CSS, HTML, JavaScript
17-
alert( arr ); // HTML, JavaScript, CSS (no changes)
16+
alert(sorted); // CSS, HTML, JavaScript
17+
alert(arr); // HTML, JavaScript, CSS (no changes)
1818
```

1-js/05-data-types/05-array-methods/6-array-get-names/task.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ importance: 5
22

33
---
44

5-
# Map to names
5+
# خارطة بالأسماء
66

7-
You have an array of `user` objects, each one has `user.name`. Write the code that converts it into an array of names.
7+
لدينا مصفوفة من كائنات user، لكلّ منها صفة user.name. اكتب كودًا يحوّلها إلى مصفوفة من الأسماء.
88

9-
For instance:
9+
مثال:
1010

1111
```js no-beautify
1212
let john = { name: "John", age: 25 };
1313
let pete = { name: "Pete", age: 30 };
1414
let mary = { name: "Mary", age: 28 };
1515

16-
let users = [ john, pete, mary ];
16+
let users = [john, pete, mary];
1717

18-
let names = /* ... your code */
19-
20-
alert( names ); // John, Pete, Mary
18+
let names /* ... your code */ = alert(names); // John, Pete, Mary
2119
```
22-

0 commit comments

Comments
 (0)