技能測試:數學

本頁面的測試旨在幫助您評估是否理解了 JavaScript 中的基礎數學 — 數字和運算子 文章。

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

數學 1

讓我們開始測試您對基本數學運算子的知識。您將建立四個數值,將兩個相加,從另一箇中減去一個,然後將結果相乘。最後,您將編寫一個測試來證明該值是偶數。

完成任務

  1. 建立四個包含數字的變數。給變數起個有意義的名字。
  2. 將前兩個變數相加,並將結果儲存在另一個變數中。
  3. 將第三個變數減去第四個變數,並將結果儲存在另一個變數中。
  4. 將第 2 步和第 3 步的結果相乘,並將結果儲存在名為 finalResult 的變數中。
  5. 使用其中一個 算術運算子 檢查 finalResult 是否為偶數。將結果(偶數為 0,奇數為 1)儲存在名為 evenOddResult 的變數中。

要透過此測試,finalResult 的值應為 48evenOddResult 的值應為 0

js
let finalResult;
let evenOddResult;

// Don't edit the code above here!

// Add your code here

// Don't edit the code below here!

const section = document.querySelector("section");
const para1 = document.createElement("p");
const finalResultCheck =
  finalResult === 48 ? `Yes, well done!` : `No, it is ${finalResult}`;
para1.textContent = `Is the finalResult 48? ${finalResultCheck}`;
const para2 = document.createElement("p");
const evenOddResultCheck =
  evenOddResult === 0
    ? "The final result is even!"
    : "The final result is odd. Hrm.";
para2.textContent = evenOddResultCheck;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案

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

js
// ...
// Don't edit the code above here!

const number1 = 4;
const number2 = 8;
const number3 = 12;
const number4 = 8;

const additionResult = number1 + number2;
const subtractionResult = number3 - number4;

finalResult = additionResult * subtractionResult;

evenOddResult = finalResult % 2;

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

數學 2

在第二個任務中,您將獲得兩個計算,其結果儲存在變數 resultresult2 中。您需要對這些計算進行相乘,並將結果格式化為兩位小數。

完成任務

  1. resultresult2 相乘,並將結果重新賦給 result(使用賦值簡寫)。
  2. result 格式化為兩位小數,並將其儲存在名為 finalResult 的變數中。
  3. 使用 typeof 檢查 finalResult 的資料型別。如果它是 string,則將其轉換為 number 型別,並將結果儲存在名為 finalNumber 的變數中。

要透過此測試,finalNumber 的結果應為 4633.33。您可能需要考慮運算子優先順序,並在輸入表示式中新增或修改一些括號以獲得正確輸出。

js
// Final result should be 4633.33

let result = 7 + 13 / 9 + 7;
let result2 = (100 / 2) * 6;

// Add your code here

// Don't edit the code below here!

const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = `Your finalResult is ${finalResult}`;
const para2 = document.createElement("p");
const finalNumberCheck =
  isNaN(finalNumber) === false
    ? "finalNumber is a number type. Well done!"
    : `Oops! finalNumber is not a number.`;
para2.textContent = finalNumberCheck;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案

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

js
// Final result should be 4633.33

let result = (7 + 13 / 9) + 7;
let result2 = 100 / 2 * 6;

result *= result2;

const finalResult = result.toFixed(2);

const finalNumber = Number(finalResult);

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

數學 3

在本文的最後一個任務中,我們希望您編寫一些測試。

完成任務

  1. 有三組,每組包含一個陳述和兩個變數。對於每一組,編寫一個測試來證明或證偽所做的陳述。
  2. 將這些測試的結果分別儲存在名為 weightComparisonheightComparisonpwdMatch 的變數中。
js
// Statement 1: The elephant weighs less than the mouse
const eleWeight = 1000;
const mouseWeight = 2;
// Statement 2: The Ostrich is taller than the duck
const ostrichHeight = 2;
const duckHeight = 0.3;
// Statement 3: The two passwords match
const pwd1 = "stromboli";
const pwd2 = "stROmBoLi";

// Don't edit the code above here!

// 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");
const para3 = document.createElement("p");
const weightTest = weightComparison
  ? "True — elephants do weigh less than mice!?"
  : "False — of course an elephant is heavier than a mouse!";
const heightTest = heightComparison
  ? "True — an ostrich is indeed taller than a duck!"
  : "False — apparently a duck is taller than an ostrich!?";
const pwdTest = pwdMatch
  ? "True — the passwords match."
  : "False — the passwords do not match; please check them";
para1.textContent = weightTest;
section.appendChild(para1);
para2.textContent = heightTest;
section.appendChild(para2);
para3.textContent = pwdTest;
section.appendChild(para3);
點選此處顯示解決方案

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

js
// ...
// Don't edit the code above here!

const weightComparison = eleWeight < mouseWeight;
const heightComparison = ostrichHeight > duckHeight;
const pwdMatch = pwd1 === pwd2;

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