Test your skills: Math
The aim of the tests on this page is to assess whether you've understood the Basic 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 as CodePen, JSFiddle, or Glitch. 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 our communication 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, we need to write a check that proves that this value is an even number.
Try updating the live code below to recreate the finished example by following these steps:
- 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 steps 2 and 3 and store the result in a variable called
finalResult
. - Check if
finalResult
is an even number using one of the arithmetic operators. Store the result (0
for even,1
for odd) in a variable calledevenOddResult
.
To pass this test, finalResult
should have a value of 48
and evenOddResult
should have a value of 0
.
Download the starting point for this task to work in your own editor or in an online editor.
Math 2
In the second task, you are provided with two calculations with the results stored in the variables result
and result2
.
You need to take the calculations, multiply them, and format the result to two decimal places.
Try updating the live code below to recreate the finished example by following these steps:
- 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 of 4633.33
.
Download the starting point for this task to work in your own editor or in an online editor.
Math 3
In the final task for this article, we want you to write some tests.
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
, and pwdMatch
, respectively.
Download the starting point for this task to work in your own editor or in an online editor.