技能測試:條件語句

本次技能測試旨在幫助您評估是否已理解我們的程式碼中的決策 — 條件語句文章。

注意: 如需幫助,請閱讀我們的“技能測試”使用指南。您也可以透過我們的溝通渠道與我們聯絡。

條件語句 1

在此任務中,您將獲得兩個變數

  • season — 包含一個字串,說明當前是什麼季節。
  • response — 最初未初始化,但之後用於儲存一個將列印到輸出面板的響應。

完成任務

  1. 建立一個條件語句,檢查 season 是否包含字串 "summer",如果是,則為 response 賦值一個字串,向用戶顯示關於該季節的恰當訊息。如果不是,則應為 response 賦值一個通用字串,告知使用者我們不知道現在是什麼季節。
  2. 新增另一個條件語句,檢查 season 是否包含字串 "winter",並再次為 response 賦一個恰當的字串。
js
let season = "summer";
let response;

// Add your code here

// Don't edit the code below here!

const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = response;
section.appendChild(para1);
點選此處顯示解決方案

你完成的 JavaScript 應該看起來像這樣

js
let season = "summer";
let response;

if (season === "summer") {
  response = "It's probably nice and warm where you are; enjoy the sun!";
} else if (season === "winter") {
  response = "I hope you are not too cold. Put some warm clothes on!";
} else {
  response =
    "I don't know what the season is where you are. Hope you are well.";
}

// Don't edit the code below here!
// ...

條件語句 2

此任務提供三個變數

  • machineActive:包含一個指示器,說明答題機是否已開啟(true/false)。
  • score:包含您在一場虛擬遊戲中的得分。此分數將輸入答題機,答題機會根據分數提供一個響應,表明您表現如何。
  • response:最初未初始化,但之後用於儲存一個將列印到輸出面板的響應。

完成任務

  1. 建立一個 if...else 結構,檢查機器是否開啟,如果沒有開啟,則向 response 變數中放入一條訊息,告知使用者開啟機器。
  2. 在第一個 if...else 結構內,巢狀另一個 if...else 結構,根據 score 的值向 response 變數中放入恰當的訊息 — 如果機器已開啟。不同的條件測試(及由此產生的響應)如下:
    • 分數低於 0 或高於 100 — "這不可能,發生錯誤了。"
    • 分數 0 到 19 — "這分數太糟糕了 — 完全不及格!"
    • 分數 20 到 39 — "你懂一些東西,但分數很糟糕。需要改進。"
    • 分數 40 到 69 — "你做得還可以,不算差!"
    • 分數 70 到 89 — "這是個很高的分數,你真的很懂。"
    • 分數 90 到 100 — "真是個驚人的分數!你作弊了嗎?你是認真的嗎?"

輸入程式碼後,嘗試將 machineActive 更改為 true,並將 score 更改為幾個不同的值,看看它是否有效。請注意,在此練習的範圍內,無論 machineActive 變數的值如何,Your score is __ 字串將一直顯示在螢幕上。

js
let response;
let score = 75;
let machineActive = false;

// Add your code here

// Don't edit the code below here!

const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = `Your score is ${score}`;
para2.textContent = response;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案

你完成的 JavaScript 應該看起來像這樣

js
let response;
let score = 75;
let machineActive = false;

if (machineActive) {
  if (score < 0 || score > 100) {
    response = "This is not possible, an error has occurred.";
  } else if (score >= 0 && score < 20) {
    response = "That was a terrible score — total fail!";
  } else if (score >= 20 && score < 40) {
    response =
      "You know some things, but it's a pretty bad score. Needs improvement.";
  } else if (score >= 40 && score < 70) {
    response = "You did a passable job, not bad!";
  } else if (score >= 70 && score < 90) {
    response = "That's a great score, you really know your stuff.";
  } else if (score >= 90 && score <= 100) {
    response = "What an amazing score! Did you cheat? Are you for real?";
  }
} else {
  response = "The machine is turned off. Turn it on to process your score.";
}

// Don't edit the code below here!
// ...

條件語句 3

最後一部分任務提供四個變數

  • machineActive:包含一個指示器,說明登入機是否已開啟(true/false)。
  • pwd:包含使用者的登入密碼。
  • machineResult:最初未初始化,但之後用於儲存一個將列印到輸出面板的響應,告知使用者機器是否已開啟。
  • pwdResult:最初未初始化,但之後用於儲存一個將列印到輸出面板的響應,告知使用者其登入嘗試是否成功。

完成任務

  1. 建立一個 if...else 結構,檢查機器是否開啟,並在 machineResult 變數中放入一條訊息,告知使用者機器是開啟還是關閉。
  2. 如果機器已開啟,我們還希望執行第二個條件語句,檢查 pwd 是否等於 cheese。如果是,則應為 pwdResult 賦值一個字串,告知使用者其已成功登入。如果不是,則應為 pwdResult 賦值一個不同的字串,告知使用者其登入嘗試不成功。我們希望您在一行中完成此操作,使用非 if...else 結構。
js
let machineActive = true;
let pwd = "cheese";

let machineResult;
let pwdResult;

// Add your code here

// Don't edit the code below here!

const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = machineResult;
para2.textContent = pwdResult;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案

你完成的 JavaScript 應該看起來像這樣

js
let machineActive = true;
let pwd = "cheese";

let machineResult;
let pwdResult;

if (machineActive) {
  machineResult = "Machine is active. Trying login.";
  pwdResult =
    pwd === "cheese"
      ? "Login successful."
      : "Password incorrect; login failed.";
} else {
  machineResult = "Machine is inactive. Activate and try logging in again.";
}

// Don't edit the code below here!
// ...