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: If there is an error in your code, it will be logged into the results panel on this page (or in the browser's JavaScript console for the downloadable version) to help you. It might be helpful to copy the code into an online tool like CodePen, jsFiddle, or Glitch to test your work. If you get stuck, you can ask us for help.
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 an arithmetic operator and store the result 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
. - Update the original calculation so that they give you a value of
10.42
. Don't change the numbers or the operators.
To pass this test, finalNumber
should have a result of 10.42
.
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.
Assessment or further help
You can practice these examples in the interactive editors above. If you would like your work assessed, or are stuck and want to ask for help:
- Put your work into an online shareable editor such as CodePen, jsFiddle, or Glitch.
- Write a post asking for assessment and/or help at the MDN Discourse forum Learning category. Your post should include:
- A descriptive title such as "Assessment wanted for Math 1 skill test".
- Details of what you have already tried, and what you would like us to do, e.g. if you are stuck and need help, or want an assessment.
- A link to the example you want assessed or need help with, in an online shareable editor. This is a good practice to get into because it's very hard to help someone with a coding problem if you can't see their code.
- A link to the actual task or assessment page, so we can find the question you want help with.