JavaScript basic Functions (methods)

Source: Internet
Author: User
Tags square root

1. Mathematical method: Take random number

Math.random ()

The return value is a random number from 0 to 1, for example:

var n = math.random ();

Then 0=<n<1

Expansion: Random Extraction

var n = parseint (Math.random () * 10);
var arr = [' Bear big ', ' King II ', ' Zhang San ', ' Zhao Si ', ' Liu Wu ', ' Sun Six ', ' money Seven ', ' Li Ba Luyi ', ' Zhou Jiu ', ' Wu 10 '];
Alert (Arr[n]);

Returns an integer from 1 to 10, using the integer as the index value to extract the corresponding name in the ARR array.

2. Get time

var time = Date (); var New Date ();

Get a timestamp in the format: Week-day-month-year-hour-minute-second-time zone.

Based on the time taken, some of the values in the timestamp can be extracted using other methods:

var day = Time.getday ();//Returns the day of the week in English format
var date = Time.getdate ();//Returns a number (1~31), the date of the current month
var month = Time.getmonth () + 1;//Returns a number (0~11), month of the month, JS a bug, the month is starting from 0 number, so assign to the variable +1
var year = Time.getfullyear ();//Returns a four-digit number of years of the year

var hour = time.gethours ();//Returns a number (0~23), the current hour
var minute =time.getminutes ();//Returns a number (0~59), the current minute
var second = Time.getseconds ();//Returns a number (1~60), the current second

3. Other Mathematical methods:

A, abdication to take an integer:

var int = Math.floor (x);

The return value is an integer,

Take only the integer portion of x, no matter how large the number of decimal points are, as with parseint.

Expansion: If you do not know the method of rounding the whole function, you can x+0.5 after the use of mathematical methods Math.floor. The number obtained must be a rounded number.

B, rounding Integer:

var int = Math.ceil (x);

The return value is an integer,

Take only the integer portion of x, if there are decimals after the decimal point, no matter how small the decimal number, the integer +1.

C, take absolute value:

var int = Math.Abs (x);

The return value is a positive number,

Take the absolute value of x.

D, rounding:

var int = Math.Round (x);

The return value is an integer that takes the rounded integer value of x.

E, n power:

var int = Math.pow (x,n);

The return value is a positive number, which is the n power of X.

F, open Square

var int = MATH.SQRT (x);

The return value is a positive number, which is the square root of x.

JavaScript basic Functions (methods)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.