Find the maximum value in the array, the minimum value

Source: Internet
Author: User

Sometimes we need to find the maximum value, the minimum value, in an array.

First say the maximum value:

Find the maximum value inside the array
Let arr=[2,5,8,9,3,50,1];
function Maxnum () {
Let Max=arr[0]; If the first item is the maximum value
For a let i=1;i<arr.length;i++ {//compares the other items in the data in turn with the first item.

if (Arr[i]>max) {//If an item in the array is found to be larger than the largest item we set previously, replace it in the order
Max=arr[i];

}

}
return Max;
}
Console.log (Maxnum ())

The same minimum value:

  

function Minnum () {
Let Min=arr[0]; If the first item is the minimum value
For a let i=0;i<arr.length;i++ {//compares the other items in the data in turn with the first item.
if (arr[i]<min) {//If an item in the array is found to be less than the largest item we set previously, replace it in the order
Min=arr[i];

}

}
return min;
}
Console.log (Minnum ());

The results were:

Find the maximum value in the array, the minimum value

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.