2.8 KiB

id, title, challengeType, videoUrl, forumTopicId, localeTitle
id title challengeType videoUrl forumTopicId localeTitle
56533eb9ac21ba0edf2244d9 Comparisons with the Logical Or Operator 1 https://scrimba.com/c/cEPrGTN 16800 逻辑或运算符

Description

只要逻辑或运算符||两边任何一个为true,那么它就返回true;否则返回false逻辑或运算符由两个管道符号(|)组成。这个按键位于退格键和回车键之间。 下面这样的语句你应该很熟悉:
if (num > 10) {
  return "No";
}
if (num < 5) {
  return "No";
}
return "Yes";

只有当num大于等于 5 或小于等于 10 时,函数返回"Yes"。相同的逻辑可以简写成:

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

Instructions

请使用逻辑或运算符把两个 if 语句合并为一个 if 语句,如果val不在 10 和 20 之间(包括 10 和 20),返回"Outside"。反之,返回"Inside"

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>testLogicalOr(0)</code>应该返回 "Outside"。
    testString: assert(testLogicalOr(0) === "Outside");
  - text: <code>testLogicalOr(9)</code>应该返回 "Outside"。
    testString: assert(testLogicalOr(9) === "Outside");
  - text: <code>testLogicalOr(10)</code>应该返回 "Inside"。
    testString: assert(testLogicalOr(10) === "Inside");
  - text: <code>testLogicalOr(15)</code>应该返回 "Inside"。
    testString: assert(testLogicalOr(15) === "Inside");
  - text: <code>testLogicalOr(19)</code>应该返回 "Inside"。
    testString: assert(testLogicalOr(19) === "Inside");
  - text: <code>testLogicalOr(20)</code>应该返回 "Inside"。
    testString: assert(testLogicalOr(20) === "Inside");
  - text: <code>testLogicalOr(21)</code>应该返回 "Outside"。
    testString: assert(testLogicalOr(21) === "Outside");
  - text: <code>testLogicalOr(25)</code>应该返回 "Outside"。
    testString: assert(testLogicalOr(25) === "Outside");

Challenge Seed

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

  if (val) {
    return "Outside";
  }

  if (val) {
    return "Outside";
  }

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

// Change this value to test
testLogicalOr(15);

Solution

function testLogicalOr(val) {
  if (val < 10 || val > 20) {
    return "Outside";
  }
  return "Inside";
}