THE Math CLASS

We will repeat this because it's important:

A collection of useful and related methods is called: a Class.

To use a method that is inside a Class you need,sometimes, to create a prefix (object) that will be used to "call" the method.

We saw, in a previous lesson, the Date class. We will see now another useful class: the Math class.

IMPORTANT: In this class you will not need to create an object, a prefix. The object is always : Math.

Some classes work like this.

Using this Math prefix, the methods of the class are:

Math.abs(number)            Returns the absolute value of number. 

Math.ceil(number)           Returns the next largest  integer 
                            greater than number (rounds up).

Math.floor(number)          Returns the next smallest  integer 
                            less than the number (rounds down).

Math.pow(num1, num2)        Returns the value of num1 to the 
                            power of num2.

Math.random()               Returns a number between 0 and 1.

Math.round(number)          Returns the closest integer to the 
                            number

Math.sqrt(number)           Returns the square root of the number        
                                   
 

Now, to have fun, you can create a simple game using Math methods - a kind of Coin Toss (Heads or Tails in UK). Put these lines of code in the PostWorld.js file of the Exec1 project. Choose "1" or "0" and open the project. Look the number printed.

 
num=Math.round(Math.random());
Chat.print('The number is now: ', num );

Each time you reopen the project you can have a different number!


PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE