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 ステートメントでは、順序が重要です。
関数は上から下へと実行されるので、どのステートメントが先に来るかに気をつけるようにしてください。
例として 2 つの関数を考えてみましょう。
まず 1 つ目です。
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";
}
}
2 つ目は 1 つ目のステートメントの順序を入れ替えただけのものです。
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";
}
}
これら 2 つの関数は見かけはほとんど同じですが、両方に数値を渡すと異なる出力が得られます。
foo(0)
bar(0)
foo(0) は文字列 Less than one を返し、bar(0) は文字列 Less than two を返します。
--instructions--
すべてのケースで適切なステートメントを返すように、関数内のロジックの順序を変更してください。
--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";
}
}