Movatterモバイル変換


[0]ホーム

URL:


  1. Learn
  2. Core learning modules
  3. JavaScript
  4. Test: JavaScript tests index
  5. Test: Math

Test your skills: Math

The aim of the tests on this page is to help you assess whether you've understood theBasic math in JavaScript — numbers and operators article.

Note:To get help, read ourTest your skills usage guide. You can also reach out to us using one of ourcommunication channels.

Math 1

Let's start out by testing your knowledge of basic math operators.You will create four numeric values, add two together, subtract one from another, then multiply the results.Finally, you'll write a test to prove that this value is an even number.

To complete the task:

  1. Create four variables that contain numbers. Call the variables something sensible.
  2. Add the first two variables together and store the result in another variable.
  3. Subtract the fourth variable from the third and store the result in another variable.
  4. Multiply the results from steps2 and3 and store the result in a variable calledfinalResult.
  5. Check iffinalResult is an even number using one of thearithmetic operators. Store the result (0 for even,1 for odd) in a variable calledevenOddResult.

To pass this test,finalResult should have a value of48 andevenOddResult should have a value of0.

<section></section>
* {  box-sizing: border-box;}p {  color: purple;  margin: 0.5em 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);
Click here to show the solution

Your finished JavaScript should look something like this:

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!// ...

Math 2

In the second task, you are provided with two calculations with the results stored in the variablesresult andresult2.You need to take the calculations, multiply them, and format the result to two decimal places.

To complete the task:

  1. Multiplyresult andresult2 and assign the result back toresult (use assignment shorthand).
  2. Formatresult so that it has two decimal places and store it in a variable calledfinalResult.
  3. Check the data type offinalResult usingtypeof. If it's astring, convert it to anumber type and store the result in a variable calledfinalNumber.

To pass this test,finalNumber should have a result of4633.33. You might need to consider operator precedence and add or modify some parentheses to the input expressions to get the correct output.

js
// Final result should be 4633.33let 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);
Click here to show the solution

Your finished JavaScript should look something like this:

js
// Final result should be 4633.33let 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!// ...

Math 3

In the final task for this article, we want you to write some tests.

To complete the task:

  1. There are three groups, each consisting of a statement and two variables. For each one, write a test that proves or disproves the statement made.
  2. Store the results of those tests in variables calledweightComparison,heightComparison, andpwdMatch, respectively.
js
// Statement 1: The elephant weighs less than the mouseconst eleWeight = 1000;const mouseWeight = 2;// Statement 2: The Ostrich is taller than the duckconst ostrichHeight = 2;const duckHeight = 0.3;// Statement 3: The two passwords matchconst 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);
Click here to show the solution

Your finished JavaScript should look something like this:

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!// ...

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp