5.7.2.2 min () and max () methods, 5.7.2.2min

Source: Internet
Author: User

5.7.2.2 min () and max () methods, 5.7.2.2min

Math objects also contain many methods to help complete simple and complex mathematical calculations.

The min () and max () methods are used to determine the minimum and maximum values of a group of values. Both methods can accept any number of numeric parameters, as shown in the following example:

Var max = Math. max (3, 54, 32, 16); alert (max); // 54var min = Math. min (3, 54, 32, 16); alert (min); // 3

For 3, 54, 32, and 16, Math. max () returns 54, while Math. min () returns 3. These two methods are often used to avoid unnecessary loops and determine the maximum number of a group in the if statement.

To find the maximum or minimum values in the array, use the apply () method as follows.

Var values = [1, 2, 3, 4, 5, 6, 7, 8]; var max = Math. max. apply (Math, values );

The key to this technique is to take the Math object as the first parameter of apply () and set this value correctly. Then, any array can be used as the second parameter.

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.