Introductions
Numbers are used in JavaScript for calculations such as prices, marks, ages, percentages, measurements, and scores. JavaScript also provides the built-in Math object with useful methods for rounding numbers, finding the largest or smallest value, generating random numbers, and performing mathematical calculations. In this chapter, you will practice common number operations and Math methods through simple, practical examples. JavaScript Numbers and Math Practice questions with solutions help to understand the concepts.
Question 1: Add Two Numbers
Problem
Create two variables containing 25 and 35, then calculate and display their sum.
Solution
let number1 = 25;
let number2 = 35;
let sum = number1 + number2;
console.log(sum);
Output
60
Step-by-step Explanation
number1stores25.number2stores35.- The
+operator adds both numbers. 25 + 35gives60.- The result is stored in
sum. console.log()displays the result.
Question 2: Calculate the Remainder
Problem
Create two numbers, 25 and 4, and find the remainder after dividing the first number by the second number.
Solution
let number1 = 25;
let number2 = 4;
let remainder = number1 % number2;
console.log(remainder);
Output
1
Step-by-step Explanation
number1contains25.number2contains4.- The
%operator returns the remainder. 25 ÷ 4gives a remainder of1.- Therefore, the output is
1.
Question 3: Round a Decimal Number
Problem
A calculation produces the value 7.6. Use Math.round() to round it to the nearest whole number.
Solution
let number = 7.6;
let result = Math.round(number);
console.log(result);
Output
8
Step-by-step Explanation
- The variable
numbercontains7.6. Math.round()rounds a number to the nearest integer.- Since
7.6is closer to8than7, the result is8.
Question 4: Round a Number Down
Problem
A number is 8.9. Use Math.floor() to round it down to the nearest whole number.
Solution
let number = 8.9;
let result = Math.floor(number);
console.log(result);
Output
8
Step-by-step Explanation
numbercontains8.9.Math.floor()rounds a number down to the nearest integer.- The result is
8.
For positive numbers:
8.9 → 8
Question 5: Round a Number Up
Problem
A number is 8.1. Use Math.ceil() to round it up to the nearest whole number.
Solution
let number = 8.1;
let result = Math.ceil(number);
console.log(result);
Output
9
Step-by-step Explanation
numbercontains8.1.Math.ceil()rounds a number up to the next integer.- Therefore,
8.1becomes9.
Question 6: Find the Largest Number
Problem
Three students score 78, 92, and 85 marks. Use Math.max() to find the highest score.
Solution
let marks1 = 78;
let marks2 = 92;
let marks3 = 85;
let highest = Math.max(marks1, marks2, marks3);
console.log(highest);
Output
92
Step-by-step Explanation
- Three variables store the three scores.
Math.max()compares the values provided to it.- The largest value is
92. - The result is stored in
highest. - The program displays
92.
Question 7: Find the Smallest Number
Problem
Three temperatures are 28, 22, and 31 degrees. Use Math.min() to find the lowest temperature.
Solution
let temperature1 = 28;
let temperature2 = 22;
let temperature3 = 31;
let lowest = Math.min(temperature1, temperature2, temperature3);
console.log(lowest);
Output
22
Step-by-step Explanation
- The three temperature values are stored in variables.
Math.min()finds the smallest value.- The smallest temperature is
22. - The result is stored in
lowest. - The program displays
22.
Question 8: Calculate the Square of a Number
Problem
Create a variable containing 6 and calculate its square using Math.pow().
Solution
let number = 6;
let square = Math.pow(number, 2);
console.log(square);
Output
36
Step-by-step Explanation
numberstores6.Math.pow(number, 2)means6raised to the power of2.- The calculation is:
6 × 6 = 36
- The result is stored in
square. - The program displays
36.
You can also use the exponentiation operator:
let square = number ** 2;
Question 9: Generate a Random Number
Problem
Generate a random decimal number between 0 and 1 using JavaScript.
Solution
let randomNumber = Math.random();
console.log(randomNumber);
Output
The output will be different each time, for example:
0.724583921
Step-by-step Explanation
Math.random()generates a random number.- The generated number is greater than or equal to
0. - It is less than
1. - Because it is random, the exact output changes each time you run the program.
For example:
0.21
0.65
0.93
0.47
are all possible results.
Question 10: Generate a Random Number from 1 to 10
Problem
Write a JavaScript program that generates a random whole number between 1 and 10.
Solution
let randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);
Output
The output can be any whole number from:
1
2
3
4
5
6
7
8
9
10
Step-by-step Explanation
Math.random()generates a number from0up to, but not including,1.- Multiplying it by
10gives a value from0up to, but not including,10. Math.floor()removes the decimal part.- The result becomes a whole number from
0to9. - Adding
1changes the range to1through10.
The basic formula is:
Math.floor(Math.random() * 10) + 1
This technique is commonly used in games, quizzes, random selections, and other JavaScript programs.
Key Takeaways
- JavaScript uses the
numberdata type for numeric values. - The
+operator performs addition. - The
-operator performs subtraction. - The
*operator performs multiplication. - The
/operator performs division. - The
%operator returns the remainder. Math.round()rounds a number to the nearest integer.Math.floor()rounds down.Math.ceil()rounds up.Math.max()finds the largest value.Math.min()finds the smallest value.Math.pow()calculates a number raised to a power.Math.random()generates a random decimal number between0and1.Math.floor()andMath.random()can be combined to generate random whole numbers.
FAQs
1. What is the Math object in JavaScript?
Math is a built-in JavaScript object that provides properties and methods for performing mathematical operations.
2. What does Math.round() do?
Math.round() rounds a number to the nearest whole number.
console.log(Math.round(5.6));
Output:
6
3. What is the difference between Math.floor() and Math.ceil()?
Math.floor() rounds down, while Math.ceil() rounds up.
console.log(Math.floor(5.8));
console.log(Math.ceil(5.2));
Output:
5
6
4. How can I find the largest number in JavaScript?
Use Math.max().
console.log(Math.max(10, 25, 15));
Output:
25
5. How can I find the smallest number in JavaScript?
Use Math.min().
console.log(Math.min(10, 25, 15));
Output:
10
6. What does Math.random() return?
Math.random() returns a pseudo-random decimal number greater than or equal to 0 and less than 1.
7. How can I generate a random number between 1 and 10?
You can use:
let number = Math.floor(Math.random() * 10) + 1;
console.log(number);
The result will be a whole number from 1 to 10.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
