Textbook Chapter 6: Functions and Procedures
Built-in Functions and Methods
methods. As we've learned, a method is a function that is defined within an
object. To access the
abs method of the Math object, we can use the syntax:
Math.abs(). The table below identifies several of the common mathematical methods
|Abs||Returns the absolute value of a number||Math.abs(-2)||2|
|Floor||Rounds number to next lowest integer||Math.floor(7.1)||7|
|Ceil||Rounds number to next highest integer||Math.ceil(7.1)||8|
|Random||Returns a random number between 0 and 1||Math.random()||varies|
|Round||Rounds the number to the nearest integer||Math.round(7.5)||8|
|Min||Returns the lowest of two numbers||Math.min(3,4)||3|
|Max||Returns the highest of two numbers||Math.max(3,4)||4|
The following code uses the random method of the math object to select a random number between 0 and 1.
Check out the EXAMPLE.