mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-03 06:03:23 -05:00
2.3 KiB
2.3 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| 5690307fddb111c6084545d7 | الترتيب المنطقي في تعبيرات If Else | 1 | https://scrimba.com/c/cwNvMUV | 18228 | logical-order-in-if-else-statements |
--description--
الترتيب مهم في عبارات if و else if.
يتم تنفيذ الوظيفة (function) من الأعلى إلى الأسفل، لذا سترغب في توخي الحذر بشأن العبارة التي تأتي أولاً.
ولنأخذ هاتين الوظيفتين كمثال.
إليك الأول:
function foo(x) {
if (x < 1) {
return "Less than one";
} else if (x < 2) {
return "Less than two";
} else {
return "Greater than or equal to two";
}
}
أما الثاني فقد غير ترتيب العبارات فقط:
function bar(x) {
if (x < 2) {
return "Less than two";
} else if (x < 1) {
return "Less than one";
} else {
return "Greater than or equal to two";
}
}
بينما تبدو هاتان الوظيفتان متطابقتان تقريباً، إذا اعطينا رَقم إلى كليهما نحصل على نواتج مختلفة.
foo(0)
bar(0)
سوف ينتج foo(0) المقطع Less than one، وسوف ينتج bar(0) المقطع Less than two.
--instructions--
غيّر ترتيب المنطق في الوظيفة (function) بحيث تنتج العبارات الصحيحة في جميع الحالات.
--hints--
يجب أن تنتج orderMyLogic(4) المقطع Less than 5
assert(orderMyLogic(4) === 'Less than 5');
يجب أن تنتج orderMyLogic(6) المقطع Less than 10
assert(orderMyLogic(6) === 'Less than 10');
يجب أن تنتج orderMyLogic(11) المقطع Greater than or equal to 10
assert(orderMyLogic(11) === 'Greater than or equal to 10');
--seed--
--seed-contents--
function orderMyLogic(val) {
if (val < 10) {
return "Less than 10";
} else if (val < 5) {
return "Less than 5";
} else {
return "Greater than or equal to 10";
}
}
orderMyLogic(7);
--solutions--
function orderMyLogic(val) {
if(val < 5) {
return "Less than 5";
} else if (val < 10) {
return "Less than 10";
} else {
return "Greater than or equal to 10";
}
}