freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-an.../basic-javascript/comparisons-with-the-logica...

2.2 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244d8 邏輯與運算符 1 https://scrimba.com/c/cvbRVtr 16799 comparisons-with-the-logical-and-operator

--description--

有時你需要在一次判斷中做多個操作。 當且僅當運算符的左邊和右邊都是 true邏輯與運算符(&&)纔會返回 true

同樣的效果可以通過 if 語句的嵌套來實現:

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

只有當 num 的值大於 5 並且小於10 時纔會返回 Yes。 相同的邏輯可被寫爲:

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

--instructions--

請使用 && 運算符把兩個 if 語句合併爲一個 if 語句,如果 val 小於或等於 50 並且大於或等於 25 時,返回 Yes。 否則,將返回 No

--hints--

你應該使用 && 運算符一次。

assert(code.match(/&&/g).length === 1);

你應該只有一個 if 表達式。

assert(code.match(/if/g).length === 1);

testLogicalAnd(0) 應該返回字符串 No

assert(testLogicalAnd(0) === 'No');

testLogicalAnd(24) 應該返回字符串 No

assert(testLogicalAnd(24) === 'No');

testLogicalAnd(25) 應該返回字符串 Yes

assert(testLogicalAnd(25) === 'Yes');

testLogicalAnd(30) 應該返回字符串 Yes

assert(testLogicalAnd(30) === 'Yes');

testLogicalAnd(50) 應該返回字符串 Yes

assert(testLogicalAnd(50) === 'Yes');

testLogicalAnd(51) 應該返回字符串 No

assert(testLogicalAnd(51) === 'No');

testLogicalAnd(75) 應該返回字符串 No

assert(testLogicalAnd(75) === 'No');

testLogicalAnd(80) 應該返回字符串 No

assert(testLogicalAnd(80) === 'No');

--seed--

--seed-contents--

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

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

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

testLogicalAnd(10);

--solutions--

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