-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
title: استخدم العامل الشرطي الثلاثي | ||
snippet: قد تتذكر من المقارنة مع عامل المساواة أن جميع عوامل المقارنة تُرجع قيمة منطقية صحيحة أو خاطئة | ||
order: 12 | ||
--- | ||
|
||
يمكن استخدام العامل الشرطي، والذي يُسمى أيضًا العامل الثلاثي (`Ternary operator`)، | ||
كتعبير `if-else` ذو السطر الواحد. | ||
|
||
تتكون جمله العامل الثلاثي من `a ? b : c`، حيث a هو الشرط، و `b` هو الكود الذي | ||
سيتم تشغيله عندما يعود الشرط `صحيحًا`، و `c` هو الكود الذي سيتم تشغيله عندما يعود | ||
الشرط `خطأ`. | ||
|
||
تستخدم الدالة التالية عبارة `if/else` للتحقق من الشرط: | ||
|
||
```js | ||
function findGreater(a, b) { | ||
if (a > b) { | ||
return "a is greater"; | ||
} else { | ||
return "b is greater or equal"; | ||
} | ||
} | ||
``` | ||
|
||
يمكن إعادة كتابة ذلك باستخدام العامل الشرطي: | ||
|
||
```js | ||
function findGreater(a, b) { | ||
return a > b ? "a is greater" : "b is greater or equal"; | ||
} | ||
``` | ||
|
||
## استخدم عوامل تشغيل شرطية (ثلاثية) متعددة | ||
|
||
في القسم السابق، استخدمت عامل شرطي واحد. يمكنك أيضًا ربطها معًا للتحقق من وجود | ||
شروط متعددة. | ||
|
||
تستخدم الدالة التالية عبارات `if` و `else if` و `else` للتحقق من شروط متعددة: | ||
|
||
```js | ||
function findGreaterOrEqual(a, b) { | ||
if (a === b) { | ||
return "a and b are equal"; | ||
} else if (a > b) { | ||
return "a is greater"; | ||
} else { | ||
return "b is greater"; | ||
} | ||
} | ||
``` | ||
|
||
يمكن إعادة كتابة الدالة أعلاه باستخدام عوامل شرطية متعددة: | ||
|
||
```js | ||
function findGreaterOrEqual(a, b) { | ||
return (a === b) | ||
? "a and b are equal" | ||
: (a > b) | ||
? "a is greater" | ||
: "b is greater"; | ||
} | ||
``` | ||
|
||
<mark> | ||
من أفضل الممارسات تنسيق عوامل تشغيل شرطية متعددة بحيث يكون كل شرط في سطر منفصل، كما هو موضح أعلاه. قد يؤدي استخدام عوامل تشغيل شرطية متعددة بدون مسافة بادئة مناسبة إلى صعوبة قراءة التعليمات البرمجية الخاصة بك. كما هو موضح في الدالة التالية.: | ||
</mark> | ||
|
||
```js | ||
function findGreaterOrEqual(a, b) { | ||
return (a === b) | ||
? "a and b are equal" | ||
: (a > b) | ||
? "a is greater" | ||
: "b is greater"; | ||
} | ||
``` | ||
|
||
<div class="quiz"> | ||
نعتذر عن عدم وجود اختبار لهذا الدرس حالياً. نحن نعمل بجد لإعداد اختبارات لجميع الدروس وسنقوم بتوفيرها في أقرب وقت ممكن. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"label": "الحلقات", | ||
"lableSlug": "loops", | ||
"order": 14 | ||
"order": 15 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"label": "الكائنات", | ||
"lableSlug": "objects", | ||
"order": 12 | ||
"order": 13 | ||
} |