Number 3/3
Other Math Functions
JavaScript has a library by the name of Math
that has many built-in mathematics functions and methods. Letβs look at a few of them.
Math.random()
:
TheMath.random()
is a function in JavaScript, that gives random numbers between0
and1
but not including1
, for example.
123console.log(Math.random()); console.log(Math.random()); console.log(Math.random());
As you can see we get three different numbers between 0
and 1
.
Note:
You may get different output as Math.random()
gives random numbers.
Math.max()
andMath.min()
:
TheMath.max()
returns the greatest number of all the numbers passed in as an argument whileMath.min()
returns the smallest one.
12console.log(Math.max(3, 5, -10, 100, 1)); console.log(Math.min(1, 2, 6, 0, -15));
Math.pow(base, power)
:
TheMath.pow(base, power)
returnsbase
raised to the givenpower
, for example.
1console.log(Math.pow(2, 10));
Swipe to start coding
The task is to find the volume of a cylinder. The radius
and height
variables are given to storing the radius and the height of the cylinder. The formula for the volume of the cylinder is:
V = ΟhrΒ²
where Ο is the Math PI having the value of 3.1415
, r
is the radius, and h
is the height. Your task is to create a new const
variable named PI
and store 3.1415
in it and implement this formula storing the result in a variable named volume
.
Solution
Summary
- JavaScript provides the facility of rounding numbers.
- We can check whether a variable is finite or infinite through the
isFinite()
function. - We can check whether a variable is a number or not through the
isNaN()
function. - The
Math.random()
function is used to get a random numeric value from0
to1
(excluding1
).
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2
Number 3/3
Swipe to show menu
Other Math Functions
JavaScript has a library by the name of Math
that has many built-in mathematics functions and methods. Letβs look at a few of them.
Math.random()
:
TheMath.random()
is a function in JavaScript, that gives random numbers between0
and1
but not including1
, for example.
123console.log(Math.random()); console.log(Math.random()); console.log(Math.random());
As you can see we get three different numbers between 0
and 1
.
Note:
You may get different output as Math.random()
gives random numbers.
Math.max()
andMath.min()
:
TheMath.max()
returns the greatest number of all the numbers passed in as an argument whileMath.min()
returns the smallest one.
12console.log(Math.max(3, 5, -10, 100, 1)); console.log(Math.min(1, 2, 6, 0, -15));
Math.pow(base, power)
:
TheMath.pow(base, power)
returnsbase
raised to the givenpower
, for example.
1console.log(Math.pow(2, 10));
Swipe to start coding
The task is to find the volume of a cylinder. The radius
and height
variables are given to storing the radius and the height of the cylinder. The formula for the volume of the cylinder is:
V = ΟhrΒ²
where Ο is the Math PI having the value of 3.1415
, r
is the radius, and h
is the height. Your task is to create a new const
variable named PI
and store 3.1415
in it and implement this formula storing the result in a variable named volume
.
Solution
Summary
- JavaScript provides the facility of rounding numbers.
- We can check whether a variable is finite or infinite through the
isFinite()
function. - We can check whether a variable is a number or not through the
isNaN()
function. - The
Math.random()
function is used to get a random numeric value from0
to1
(excluding1
).
Thanks for your feedback!
Awesome!
Completion rate improved to 2single