The aim of the tests on this page is to help you assess whether you've understood the Basic math in JavaScript â numbers and operators article.
Note: To get help, read our Test your skills usage guide. You can also reach out to us using one of our communication channels.
Math 1Let'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:
finalResult
.finalResult
is an even number using one of the arithmetic operators. Store the result (0
for even, 1
for odd) in a variable called evenOddResult
.To pass this test, finalResult
should have a value of 48
and evenOddResult
should have a value of 0
.
* {
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 variables result
and result2
. You need to take the calculations, multiply them, and format the result to two decimal places.
To complete the task:
result
and result2
and assign the result back to result
(use assignment shorthand).result
so that it has two decimal places and store it in a variable called finalResult
.finalResult
using typeof
. If it's a string
, convert it to a number
type and store the result in a variable called finalNumber
.To pass this test, finalNumber
should have a result of 4633.33
. You might need to consider operator precedence and add or modify some parentheses to the input expressions to get the correct output.
// Final result should be 4633.33
let 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.33
let 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:
weightComparison
, heightComparison
, and pwdMatch
, respectively.// Statement 1: The elephant weighs less than the mouse
const eleWeight = 1000;
const mouseWeight = 2;
// Statement 2: The Ostrich is taller than the duck
const ostrichHeight = 2;
const duckHeight = 0.3;
// Statement 3: The two passwords match
const 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!
// ...
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4