The method of finding the maximum minimum value of an array is suitable for any array _javascript technique

Source: Internet
Author: User

Today we share a method that is for arrays. In our daily work, we may have a headache when we encounter an array of problems. In particular, let's look for the maximum minimum value from a grotesque array. Although we have described a method for finding the maximum minimum of an array, the method is only used for numeric values in arrays.

Next, the method that I'm going to introduce applies to any array, and this method picks out the values in the array, which is larger than the size.
HTML code:

Copy Code code as follows:

<div id= "box" >
<b>[' 1200px ', #, ' abc ', 4, 90, [], ' 12.56 ', function () {}, ' xyz ' -30, True, number (' abc ')]</b>
<p> maximum and minimum values are: <input id= "Inpu" type= "button" value= "Display"/></p>
</div>

HTML preview:

JavaScript code:
Copy Code code as follows:

var arr = [' 1200px ', +, ' abc ', 4, 90, [], ' 12.56 ', function () {}, ' xyz ' -30, True, number (' abc ')];
/*
Find the maximum and minimum values: 1200, 4
*/
Window.onload = function () {
var arr = [' 1200px ', +, ' abc ', 4, 90, [], ' 12.56 ', function () {}, ' xyz ' -30, True, number (' abc ')];

/*
Getminmax method of obtaining maximum minimum value
Arr array gets the maximum minimum value
Num number can be passed to big or small
Returns the number argument if big returns the maximum value, small returns the minimum value
*/
function Getminmax (arr,num) {
var big = parseint (Arr[0]),
small = parseint (Arr[0]),
i = 0,
Al = Arr.length;

for (i = 0; i < al; i++) {
if (parseint (Arr[i]) > Big) {
Big = parseint (Arr[i])

else if (parseint (Arr[i]) < small) {
small = parseint (Arr[i]);
}
}
if (num== "big") return to big;
else if (num== "small") return small;
}
Click on the display, the maximum value of 12 or the minimum value of 4
document.getElementById ("Inpu"). onclick = function () {
This.parentNode.innerHTML = "2. The maximum and minimum values are:" + Getminmax (arr, "big") + "," + Getminmax (arr, "small");
}
}

Preview Effect:

The above is the smallest method of array that I share. Hope to help you. What advice can we talk about at any time, huh? ~

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.