2.2 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
5690307fddb111c6084545d7 Logical Order in If Else Statements 1 如果其他陈述中的逻辑顺序

Description

订单在if else if语句中很重要。该函数从上到下执行,因此您需要注意首先出现的语句。以这两个函数为例。这是第一个:
function foox{
ifx <1{
返回“少于一个”;
} else ifx <2{
返回“少于两个”;
} else {
返回“大于或等于2”;
}
}
第二个只是切换语句的顺序:
功能栏x{
ifx <2{
返回“少于两个”;
} else ifx <1{
返回“少于一个”;
} else {
返回“大于或等于2”;
}
}
虽然如果我们将数字传递给两者,这两个函数看起来几乎相同但我们得到不同的输出。
foo0//“不到一个”
bar0//“少于两个”

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