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:

  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 steps 2 and 3 and store the result in a variable called finalResult.
  5. Check if finalResult is an even number using an arithmetic operator and store the result in a variable called evenOddResult.

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:

  1. Multiply result and result2 and assign the result back to result (use assignment shorthand).
  2. Format result so that it has two decimal places and store it in a variable called finalResult.
  3. Check the data type of finalResult using typeof. If it's a string, convert it to a number type and store the result in a variable called finalNumber.
  4. 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:

  1. Put your work into an online shareable editor such as CodePen, jsFiddle, or Glitch.
  2. 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.