freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-an.../basic-javascript/use-conditional-logic-with-...

2.4 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
cf1111c1c12feddfaeb3bdef 用 if 語句來表達條件邏輯 1 https://scrimba.com/c/cy87mf3 18348 use-conditional-logic-with-if-statements

--description--

If 語句用於在代碼中做條件判斷。 關鍵字 if 告訴 JavaScript 在小括號中的條件爲真的情況下去執行定義在大括號裏面的代碼。 這種條件被稱爲 Boolean 條件,因爲他們只可能是 true(真)或 false(假)。

當條件的計算結果爲 true,程序執行大括號內的語句。 當布爾條件的計算結果爲 false,大括號內的代碼將不會執行。

僞代碼

if條件爲真{
語句被執行
}

示例

function test (myCondition) {
  if (myCondition) {
     return "It was true";
  }
  return "It was false";
}
test(true);
test(false);

test(true) 返回字符串 It was truetest(false) 返回字符串 It was false

test 被調用,並且傳遞進來的參數值爲 true 時,if 語句會計算 myCondition 的結果,看它是否爲 true。 如果條件爲 true,函數會返回 It was true。 當 test 被調用,並且傳遞進來的參數值爲 false 時,myCondition true,並且不執行大括號後面的語句,函數返回 It was false

--instructions--

在函數內部創建一個 if 語句,如果該參數 wasThatTrue 值爲 true,返回 Yes, that was true,否則,返回No, that was false

--hints--

trueOrFalse 應該是一個函數

assert(typeof trueOrFalse === 'function');

trueOrFalse(true) 應該返回一個字符串

assert(typeof trueOrFalse(true) === 'string');

trueOrFalse(false) 應該返回一個字符串

assert(typeof trueOrFalse(false) === 'string');

trueOrFalse(true) 應該返回 Yes, that was true

assert(trueOrFalse(true) === 'Yes, that was true');

trueOrFalse(false)應該返回 No, that was false

assert(trueOrFalse(false) === 'No, that was false');

--seed--

--seed-contents--

function trueOrFalse(wasThatTrue) {
  // Only change code below this line



  // Only change code above this line

}

--solutions--

function trueOrFalse(wasThatTrue) {
  if (wasThatTrue) {
    return "Yes, that was true";
  }
  return "No, that was false";
}