You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- 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
+
- إن كان كذلك… نُهمله، وإن لم يكن، نُضيفه إلى المصفوفة.
4
5
5
6
```js run demo
6
7
functionunique(arr) {
@@ -15,25 +16,23 @@ function unique(arr) {
15
16
return result;
16
17
}
17
18
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"
20
29
];
21
30
22
-
alert(unique(strings)); // Hare, Krishna, :-O
31
+
alert(unique(strings)); // Hare, Krishna, :-O
23
32
```
24
33
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 حالة مقارنة.
36
35
37
-
So the solution is only good for small arrays.
36
+
إلى هنا لا مشكلة، لأنّ محرّكات جافاسكربت سريعة جدًا، والمرور على 1000 عنصر في المصفوفة يحدث في بضعة ميكروثوان. ولكنّا هنا في حلقة for نُجري هذه الشروط لكلّ عنصر من arr. فإن كانت arr.length تساوي 10000 فيعني أنّا سنُجري 10000\*10000 = مئة مليون حالة مقارنة. كثير جدًا.
38
37
39
-
Further in the chapter <info:map-set> we'll see how to optimize it.
38
+
إذًا، فهذا الحل ينفع للمصفوفات الصغيرة فقط. سنرى لاحقًا في الفصل كيف نحسّن هذا الكود
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/2-filter-range/task.md
+6-9Lines changed: 6 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,18 @@ importance: 4
2
2
3
3
---
4
4
5
-
# Filter range
5
+
# نطاق ترشيح
6
6
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 وتُعيد مصفوفة بها. يجب ألّا تُعدّل الدالة المصفوفة، بل إعادة مصفوفة جديدة.
8
8
9
-
The function should not modify the array. It should return the new array.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/3-filter-range-in-place/task.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,18 @@ importance: 4
2
2
3
3
---
4
4
5
-
# Filter range "in place"
5
+
# نطاق ترشيح «كما هو»
6
6
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.
8
8
9
-
The function should only modify the array. It should not return anything.
9
+
يجب أن تُعدّل الدالة المصفوفة، ولا تُعيد شيئًا.
10
+
11
+
مثال:
10
12
11
-
For instance:
12
13
```js
13
14
let arr = [5, 3, 8, 1];
14
15
15
16
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
0 commit comments