2.2 KiB
2.2 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
5690307fddb111c6084545d7 | Logical Order in If Else Statements | 1 | 如果其他陈述中的逻辑顺序 |
Description
if
, else if
语句中很重要。该函数从上到下执行,因此您需要注意首先出现的语句。以这两个函数为例。这是第一个: function foo(x){第二个只是切换语句的顺序:
if(x <1){
返回“少于一个”;
} else if(x <2){
返回“少于两个”;
} else {
返回“大于或等于2”;
}
}
功能栏(x){虽然如果我们将数字传递给两者,这两个函数看起来几乎相同但我们得到不同的输出。
if(x <2){
返回“少于两个”;
} else if(x <1){
返回“少于一个”;
} else {
返回“大于或等于2”;
}
}
foo(0)//“不到一个”
bar(0)//“少于两个”
Instructions
Tests
tests:
- text: <code>orderMyLogic(4)</code>应返回“小于5”
testString: 'assert(orderMyLogic(4) === "Less than 5", "<code>orderMyLogic(4)</code> should return "Less than 5"");'
- text: <code>orderMyLogic(6)</code>应该返回“少于10”
testString: 'assert(orderMyLogic(6) === "Less than 10", "<code>orderMyLogic(6)</code> should return "Less than 10"");'
- text: <code>orderMyLogic(11)</code>应该返回“大于或等于10”
testString: 'assert(orderMyLogic(11) === "Greater than or equal to 10", "<code>orderMyLogic(11)</code> should return "Greater than or equal to 10"");'
Challenge Seed
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";
}
}
// Change this value to test
orderMyLogic(7);
Solution
// solution required