数学 1
让我们开始测试您对基本数学运算符的知识。您将创建四个数值,将两个相加,从另一个中减去一个,然后将结果相乘。最后,您将编写一个测试来证明该值是偶数。
完成任务
- 创建四个包含数字的变量。给变量起个有意义的名字。
- 将前两个变量相加,并将结果存储在另一个变量中。
- 将第三个变量减去第四个变量,并将结果存储在另一个变量中。
- 将第 2 步和第 3 步的结果相乘,并将结果存储在名为
finalResult
的变量中。 - 使用其中一个 算术运算符 检查
finalResult
是否为偶数。将结果(偶数为0
,奇数为1
)存储在名为evenOddResult
的变量中。
要通过此测试,finalResult
的值应为 48
,evenOddResult
的值应为 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
在第二个任务中,您将获得两个计算,其结果存储在变量 result
和 result2
中。您需要对这些计算进行相乘,并将结果格式化为两位小数。
完成任务
- 将
result
和result2
相乘,并将结果重新赋给result
(使用赋值简写)。 - 将
result
格式化为两位小数,并将其存储在名为finalResult
的变量中。 - 使用
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
在本文的最后一个任务中,我们希望您编写一些测试。
完成任务
- 有三组,每组包含一个陈述和两个变量。对于每一组,编写一个测试来证明或证伪所做的陈述。
- 将这些测试的结果分别存储在名为
weightComparison
、heightComparison
和pwdMatch
的变量中。
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!
// ...