Test your skills: Math
The aim of the tests on this page is to assess whether you've understood theBasic math in JavaScript — numbers and operators article.
Note:You can try solutions in the interactive editors on this page or in an online editor such asCodePen orJSFiddle.If there is an error in your code, it will be logged into the results panel on this page or in the JavaScript console.
If you get stuck, you can reach out to us in 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:
- Click"Play" in the code block below to edit the example in the MDN Playground.
- Create four variables that contain numbers. Call the variables something sensible.
- Add the first two variables together and store the result in another variable.
- Subtract the fourth variable from the third and store the result in another variable.
- Multiply the results from steps2 and3 and store the result in a variable called
finalResult
. - Check if
finalResult
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
.
You can alsodownload the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using theReset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
<section></section>
* { box-sizing: border-box;}p { color: purple; margin: 0.5em 0;}
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:
// ...// 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:
- Click"Play" in the code block below to edit the example in the MDN Playground.
- Multiply
result
andresult2
and assign the result back toresult
(use assignment shorthand). - Format
result
so that it has two decimal places and store it in a variable calledfinalResult
. - Check the data type of
finalResult
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.
You can alsodownload the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using theReset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
// 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:
// 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:
- Click"Play" in the code block below to edit the example in the MDN Playground.
- 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.
- Store the results of those tests in variables called
weightComparison
,heightComparison
, andpwdMatch
, respectively.
You can alsodownload the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using theReset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
// 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:
// ...// 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!// ...