Skip to content

Commit d7b712c

Browse files
authored
Merge pull request #16 from mostafaHegab/master
translate part 1 2.12, 2.13
2 parents 83e6923 + a42c099 commit d7b712c

15 files changed

Lines changed: 218 additions & 220 deletions

File tree

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# Nullish coalescing operator '??'
1+
# Nullish Coalescing Operator '??'
22

33
[recent browser="new"]
44

5-
The nullish coalescing operator `??` provides a short syntax for selecting a first "defined" variable from the list.
5+
Nullish Coalescing Operator `??` يقدم طريقة مختصرة لإختيار أول قيمة معرفة من قائمة متغيرات.
66

7-
The result of `a ?? b` is:
8-
- `a` if it's not `null` or `undefined`,
9-
- `b`, otherwise.
7+
نتيجة `a ?? b` تكون:
8+
- `a` إذا لم تكن `null` أو `undefined`,
9+
- `b`, غير ذلك.
1010

11-
So, `x = a ?? b` is a short equivalent to:
11+
لذلك فإن `x = a ?? b` طريقة مختصرة للتالي:
1212

1313
```js
1414
x = (a !== null && a !== undefined) ? a : b;
1515
```
1616

17-
Here's a longer example.
17+
هذا مثال أطول.
1818

19-
Let's say, we have a `firstName`, `lastName` or `nickName`, all of them optional.
19+
لنفترض أن لدينا `firstName`, `lastName` أو `nickName` وجميعهم اختياريين.
2020

21-
Let's choose the defined one and show it (or "Anonymous" if nothing is set):
21+
لنختار القيمة المعرفة ونعرضها (أو نعرض "Anonymous" إذا لم يحدد أي شئ):
2222

2323
```js run
2424
let firstName = null;
@@ -29,25 +29,25 @@ let nickName = "Supercoder";
2929
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
3030
```
3131
32-
## Comparison with ||
32+
## المقارنة مع ||
3333
34-
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
34+
هذا مشابه جدًا للمعامل `||`. في الحقيقة يمكننا استبدال `??` ب `||` في المثال السابق وسنحصل على نفس النتيجة.
3535
36-
The important difference is that:
37-
- `||` returns the first *truthy* value.
38-
- `??` returns the first *defined* value.
36+
الفرق الجوهري بينهما أن:
37+
- `||` يرجع أول قيمة *truthy*.
38+
- `??` يرجع أول قيمة *defined*.
3939
40-
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
40+
هذا مهم جدًا عندما نريد معاملة `null/undefined` بطريقة مختلفة عن `0`.
4141
42-
For example:
42+
مثلًا:
4343
4444
```js
4545
height = height ?? 100;
4646
```
4747
48-
This sets `height` to `100` if it's not defined. But if `height` is `0`, then it remains "as is".
48+
هذا يجعل `height` يساوي `100` إذا لم يعرف. ولكن إذا كان `height` يساوي `0` سيبقى كما هو.
4949
50-
Let's compare it with `||`:
50+
لنقارنه مع `||`:
5151
5252
```js run
5353
let height = 0;
@@ -56,62 +56,62 @@ alert(height || 100); // 100
5656
alert(height ?? 100); // 0
5757
```
5858
59-
Here, `height || 100` treats zero height as unset, same as `null`, `undefined` or any other falsy value, depeding on use cases that may be incorrect.
59+
هنا `height || 100` تعامل الصفر مثل `null`, `undefined` أو أي قيمة falsy أخرىوهذا قد لا يكون صحيح أحيانًا.
6060
61-
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`.
61+
ولكن `height ?? 100` ترجع `100` إذا كان فقط `height` يساوي تمامًا `null` أو `undefined`.
6262
63-
## Precedence
63+
## الأولوية
6464
65-
The precedence of the `??` operator is rather low: `7` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
65+
أولوية المعامل `??` هي قليلة: `7` وتساوي [MDN جدول](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
6666
67-
That's lower than most operators and a bit higher than `=` and `?`.
67+
هذا أقل من معظم المعاملات وأكبر بقليل من `=` و `?`.
6868
69-
So if we need to use `??` in a complex expression, then consider adding parentheses:
69+
لذلك إذا أردنا استخدام `??` في تعبيرات معقدة نقوم بإضافة أقواس:
7070
7171
```js run
7272
let height = null;
7373
let width = null;
7474

75-
// important: use parentheses
75+
// مهم: استخدم الأقواس
7676
let area = (height ?? 100) * (width ?? 50);
7777

7878
alert(area); // 5000
7979
```
8080
81-
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
81+
إذا لم نستخدم الأقواس فإن `*` له أولوية أعلى وسينفذ أولًا كأننا كتبنا:
8282
8383
```js
84-
// not correct
84+
// غير صحيح
8585
let area = height ?? (100 * width) ?? 50;
8686
```
8787
88-
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
88+
هناك أيضًا قيود لغوية. لأسباب أمنية لا يمكن استخدام `??` مع `&&` أو `||`.
8989
90-
The code below triggers a syntax error:
90+
هذا سينتج خطأ لغوي:
9191
9292
```js run
9393
let x = 1 && 2 ?? 3; // Syntax error
9494
```
9595
96-
The limitation is surely debatable, but for some reason it was added to the language specification.
96+
هذا القيد قد لا يبدو منطقيًا ولكن لبعض الأسباب تم إضافته للغة.
9797
98-
Use explicit parentheses to fix it:
98+
استخدم الأقواس لتجنب الخطأ:
9999
100100
```js run
101-
let x = (1 && 2) ?? 3; // Works
101+
let x = (1 && 2) ?? 3; // تعمل دون مشاكل
102102
alert(x); // 2
103103
```
104104
105-
## Summary
105+
## ملخص
106106
107-
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
107+
- معامل حذف null `??` يقدم طريقة مختصرة لإختيار أول قيمة معرفة من قائمة قيم.
108108
109-
It's used to assign default values to variables:
109+
يستخدم لوضع قيم افتراضية للمتغيرات:
110110
111111
```js
112-
// set height=100, if height is null or undefined
112+
// اجعل height=100 إذا كان null أو undefined
113113
height = height ?? 100;
114114
```
115115
116-
- The operator `??` has a very low precedence, a bit higher than `?` and `=`.
117-
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
116+
- المعامل `??` لديه أولوية قليلة جدًا لكن أعلة قليلًا من `?` و `=`.
117+
- يمنع استخدامه مع `||` أو `&&` بدون أقواس.
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `1`.
1+
الإجابة: `1`.
22

33
```js run
44
let i = 3;
@@ -8,18 +8,18 @@ while (i) {
88
}
99
```
1010

11-
Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`.
11+
كل تكرار يقوم بتقليل قيمة `i` بمقدار `1`. وتقوم `while(i)` بإيقاف الحلقة عندما `i = 0`.
1212

13-
Hence, the steps of the loop form the following sequence ("loop unrolled"):
13+
وهكذا تكون الخطوات كالتالي:
1414

1515
```js
1616
let i = 3;
1717

18-
alert(i--); // shows 3, decreases i to 2
18+
alert(i--); // عرض 3, تقليل i إلى 2
1919

20-
alert(i--) // shows 2, decreases i to 1
20+
alert(i--) // عرض 2, تقليل i إلى 1
2121

22-
alert(i--) // shows 1, decreases i to 0
22+
alert(i--) // عرض 1, تقليل i إلى 0
2323

24-
// done, while(i) check stops the loop
24+
// تم, while(i) تتوقف الحلقة
2525
```

1-js/02-first-steps/13-while-for/1-loop-last-value/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
importance: 3
1+
الأهمية: 3
22

33
---
44

5-
# Last loop value
5+
# آخر قيمة للحلقة
66

7-
What is the last value alerted by this code? Why?
7+
ما هي آخر قيمة سيتم عرضها ؟ ولماذا ؟
88

99
```js
1010
let i = 3;
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons.
1+
السؤال يوضح كيف يمكن للصيغ postfix/prefix أن تؤدي إلى نتائج مختلفة عندما تستخدم للمقارنة.
22

3-
1. **From 1 to 4**
3+
1. **من 1 إلى 4**
44

55
```js run
66
let i = 0;
77
while (++i < 5) alert( i );
88
```
99

10-
The first value is `i = 1`, because `++i` first increments `i` and then returns the new value. So the first comparison is `1 < 5` and the `alert` shows `1`.
10+
أول قيمة هي `i = 1` لأن `++i` أولًا تزيد `i` ثم تقوم بإرجاع القيمة الجديدة. لذلك فإن أول عملية مقارنة هي `1 < 5` ويقوم `alert` بعرض `1`.
1111

12-
Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable.
12+
ثم يتبع ب `2, 3, 4…` -- واحدًا بعد الآخر. المقارنة دائما تستخدم القيمة بعد الزيادة لأن `++` قبل المتغير.
1313

14-
Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown.
15-
2. **From 1 to 5**
14+
أخيرًا `i = 4`تزيد إلى `5` والمقارنة `while(5 < 5)` تفشل وتتوقف الحلقة. لذلك لا يتم عرض `5` .
15+
2. **من 1 إلى 5**
1616

1717
```js run
1818
let i = 0;
1919
while (i++ < 5) alert( i );
2020
```
2121

22-
The first value is again `i = 1`. The postfix form of `i++` increments `i` and then returns the *old* value, so the comparison `i++ < 5` will use `i = 0` (contrary to `++i < 5`).
22+
أول قيمة هي `i = 1`. صيغة postfix `i++` تزيد `i` وترجع القيمة القديمة ولذلك تكون المقارنة `i++ < 5` تستخدم `i = 0` (على العكس من `++i < 5`).
2323

24-
But the `alert` call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`.
24+
ولكن استدعاء `alert` منفصل. فهو ينفذ بعد الزيادة والمقارنة. لذلك يحصل على القيمة الحالية `i = 1`.
2525

26-
Then follow `2, 3, 4…`
26+
ثم يتبعه `2, 3, 4…`
2727

28-
Let's stop on `i = 4`. The prefix form `++i` would increment it and use `5` in the comparison. But here we have the postfix form `i++`. So it increments `i` to `5`, but returns the old value. Hence the comparison is actually `while(4 < 5)` -- true, and the control goes on to `alert`.
28+
لنتوقف عند `i = 4`. صيغة prefix `++i` تزيدها وتستخدم `5` في المقارنة. لكن postfix `i++` تزيد `i` إلى `5`وترجع القيمة القديمة. فتكون المقارنة `while(4 < 5)` -- true وينفذ `alert`.
2929

30-
The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false.
30+
قيمة `i = 5` هي آخر قيمة لأن في الخطوة التالية `while(5 < 5)` تكون false.

1-js/02-first-steps/13-while-for/2-which-value-while/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
importance: 4
1+
الأهمية: 4
22

33
---
44

5-
# Which values does the while loop show?
5+
# ما القيم التي ستعرضها الحلقة ؟
66

7-
For every loop iteration, write down which value it outputs and then compare it with the solution.
7+
في كل تكرار قم بكتابة القيمة التي سيتم عرضها وقارن إجابتك مع الحل.
88

9-
Both loops `alert` the same values, or not?
9+
هل تقوم كلتا الحلقتين بعرض نفس القيم أم لا ؟
1010

11-
1. The prefix form `++i`:
11+
1. صيغة prefix `++i`:
1212

1313
```js
1414
let i = 0;
1515
while (++i < 5) alert( i );
1616
```
17-
2. The postfix form `i++`
17+
2. صيغة postfix `i++`
1818

1919
```js
2020
let i = 0;
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
**The answer: from `0` to `4` in both cases.**
1+
**الإجابة: من `0` إلى `4` في كلتا الحالتين.**
22

33
```js run
44
for (let i = 0; i < 5; ++i) alert( i );
55

66
for (let i = 0; i < 5; i++) alert( i );
77
```
88

9-
That can be easily deducted from the algorithm of `for`:
9+
يمكن إيجاد الحل بسهولة من خوارزمية `for`:
1010

11-
1. Execute once `i = 0` before everything (begin).
12-
2. Check the condition `i < 5`
13-
3. If `true` -- execute the loop body `alert(i)`, and then `i++`
11+
1. تنفذ لمرة واحدة `i = 0` قبل أي شئ (begin).
12+
2. تفحص الشرط `i < 5`
13+
3. لو `true` -- تنفذل الأمر `alert(i)` ثم `i++`
1414

15-
The increment `i++` is separated from the condition check (2). That's just another statement.
15+
الزيادة `i++` منفصلة عن فحص الشرط (2). هي فقط أمر آخر.
1616

17-
The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.
17+
لا يتم استخدام القيمة الراجعة من عملية الزيادة لذلك لا فرق بين `i++` و `++i`.

1-js/02-first-steps/13-while-for/3-which-value-for/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
importance: 4
1+
الأهمية: 4
22

33
---
44

5-
# Which values get shown by the "for" loop?
5+
# ما القيم التي ستعرض بواسطة الحلقة "for"?
66

7-
For each loop write down which values it is going to show. Then compare with the answer.
7+
قم بكتابة القيمة التي ستعرض في كل كل حلقة وقارنها بالحل.
88

9-
Both loops `alert` same values or not?
9+
هل ستقوم كلتا الحلقتين بعرض نفس القيم أم لا ؟
1010

11-
1. The postfix form:
11+
1. صيغة postfix:
1212

1313
```js
1414
for (let i = 0; i < 5; i++) alert( i );
1515
```
16-
2. The prefix form:
16+
2. صيغة prefix:
1717

1818
```js
1919
for (let i = 0; i < 5; ++i) alert( i );

1-js/02-first-steps/13-while-for/4-for-even/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ for (let i = 2; i <= 10; i++) {
88
}
99
```
1010

11-
We use the "modulo" operator `%` to get the remainder and check for the evenness here.
11+
نستخدم معامل باقي القسمة `%` لإيجاد باقي القسمة وفحص إذا كان الرقم زوجي.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
importance: 5
1+
الأهمية: 5
22

33
---
44

5-
# Output even numbers in the loop
5+
# قم بعرض الأرقام الزوجية داخل حلقة تكرارية
66

7-
Use the `for` loop to output even numbers from `2` to `10`.
7+
استخدم حلقة `for` لعرض الأرقام الزوجية من `2` إلى `10`.
88

99
[demo]

1-js/02-first-steps/13-while-for/5-replace-for-while/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
importance: 5
1+
الأهمية: 5
22

33
---
44

5-
# Replace "for" with "while"
5+
# استبدل "for" ب "while"
66

7-
Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same).
7+
اعد كتابة الكود التالي وقم بتبديل حلقة `for` بحلقة `while` دون تغيير الخرج.
88

99
```js run
1010
for (let i = 0; i < 3; i++) {

0 commit comments

Comments
 (0)