Today, we learnt about some Math modules which help in felicitating mathematical operations. Also we built a Random Number Generator
- Math.PI
PI - This method returns the value of PI
console.log(Math.PI) // logs 3.141592653589793
- Math.max() & Math.min()
max & min - This methods returns max or min number from the list of the numbers provided respectively
console.log(Math.max(1,2,3,4,5,6,7,8,9)) // logs 9
console.log(Math.min(1,2,3,4,5,6,7,8,9)) // logs 1
- Math.pow(x,y)
pow - This method returns the exponential of a number
console.log(Math.pow(2, 3)) // logs 8 which is equal to 2^3 // pow(x,y) returns x^y
- Math.round(x)
round - This method returns the rounded number
console.log(Math.round(10.999)) // logs 11 // rounds to nearest integer
- Math.abs(x)
abs - This method reurns the absolute value/mod of a number
console.log(Math.abs(-10.52569)) // logs 10.52569 // abs returns the positive counterpart of the number
- Math.trunc(x)
Trunc - This method returns the integral part of the number
console.log(Math.trunc(6.524)) // logs 6 // returns only the integral part of the number
// if a.bcd is a number, this will return a
- Math.random()
Random - This method reurns a random number between 0 to 1
console.log(Math.random()) // returns a random number between 0 to 1
A special formula to generate random number in a range
const max=8
const min=5
console.log(Math.floor(Math.random() * (max-min + 1))+ min)
8.Math.floor(x)
Floor Function - A function which returns biggest integer smaller than the number.
console.log(Math.floor(10.528)) // logs 10
cosnole.log(Math.floor(-10.528)) // logs -11
- Math.ceil(x)
Ceiling Function - A function which returns smallest integer bigger than the number.
console.log(Math.ceil(10.528)) // logs 11
console.log(Math.ceil(-10.528)) // logs -10
Image courtesy @Brisa-Codes