2.7 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, localeTitle
id title challengeType videoUrl forumTopicId localeTitle
56533eb9ac21ba0edf2244d8 Comparisons with the Logical And Operator 1 https://scrimba.com/c/cvbRVtr 16799 逻辑与运算符

Description

有时你需要在一次判断中做多个操作。当且仅当运算符的左边和右边都是true逻辑与 运算符(&&)才会返回true。 同样的效果可以通过 if 语句的嵌套来实现:
if (num > 5) {
  if (num < 10) {
    return "Yes";
  }
}
return "No";

只有当num的值在 6 和 9 之间(包括 6 和 9才会返回 "Yes"。相同的逻辑可被写为:

if (num > 5 && num < 10) {
  return "Yes";
}
return "No";

Instructions

请使用逻辑与运算符把两个 if 语句合并为一个 if 语句,如果val小于或等于50并且大于或等于25,返回"Yes"。否则,将返回"No"

Tests

tests:
  - text: 你应该使用<code>&&</code>运算符一次。
    testString: assert(code.match(/&&/g).length === 1,);
  - text: 你应该只有一个<code>if</code>表达式。
    testString: assert(code.match(/if/g).length === 1);
  - text: <code>testLogicalAnd(0)</code>应该返回 "No"。
    testString: assert(testLogicalAnd(0) === "No");
  - text: <code>testLogicalAnd(24)</code>应该返回 "No"。
    testString: assert(testLogicalAnd(24) === "No");
  - text: <code>testLogicalAnd(25)</code>应该返回 "Yes"。
    testString: assert(testLogicalAnd(25) === "Yes");
  - text: <code>testLogicalAnd(30)</code>应该返回 "Yes"。
    testString: assert(testLogicalAnd(30) === "Yes");
  - text: <code>testLogicalAnd(50)</code>应该返回 "Yes"。
    testString: assert(testLogicalAnd(50) === "Yes");
  - text: <code>testLogicalAnd(51)</code>应该返回 "No"。
    testString: assert(testLogicalAnd(51) === "No");
  - text: <code>testLogicalAnd(75)</code>应该返回 "No"。
    testString: assert(testLogicalAnd(75) === "No");
  - text: <code>testLogicalAnd(80)</code>应该返回 "No"。
    testString: assert(testLogicalAnd(80) === "No");

Challenge Seed

function testLogicalAnd(val) {
  // Only change code below this line

  if (val) {
    if (val) {
      return "Yes";
    }
  }

  // Only change code above this line
  return "No";
}

// Change this value to test
testLogicalAnd(10);

Solution

function testLogicalAnd(val) {
  if (val >= 25 && val <= 50) {
    return "Yes";
  }
  return "No";
}