JAVASCRIPT MATHS


To return random number between 0 and 1 using javascript

Math.random() method is used to generate any random number between 0 to 1.Number can even be in decimals.

Math.random

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.random();
</script>
</body>
</html>

To extract maximum number from a given series of numbers using javascript

Math.max()

Math.max() function is used to get maximum numbers from the given series of numbers in javascript.

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.max(12,80,90,-8);
</script>
</body>
</html>

To extract minimum number from a given series of numbers using javascript

Math.min()

Math.min() function is used to get minimum number from the given series using javascript.

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.min(12,80,90,-8);
</script>
</body>
</html>

To round a number to nearest number using javascript

Math.round()

Math.round() function can round the given number to the nearest integer.

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.round(10.13) + "<br>" +
Math.round(10.73);
</script>
</body>
</html>

To round a number to next higher number using javascript

Math.ceil()

Math.ceil() function in javascript is used to round the number to the next integer.This function never searches for nearest integer it only points toward next integer number.

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.ceil(10.13);
</script>
</body>
</html>

To round a number to previous integer number using javascript

Math.floor()

Math.floor() is used to round a number to the previous integer.This method never searches for nearest possible integer it only points to the previous integer number.

Coding:

<!DOCTYPE html>
<html>
<head>
<title>
JAVASCRIPT
</title>
</head>
<body>
<p id="doc">
</p>
<script>
document.getElementById
("doc").innerHTML= Math.floor(10.73);
</script>
</body>
</html>