To find the minimum and maximum values of an array, the average of a set of numbers, a detailed description of the sort function, an array of classes

Source: Internet
Author: User

To find the minimum and maximum values of an array

1 //finding the maximum and minimum values in an array2 vararr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30];3 //the first method based on the sorting method to find the maximum and minimum values from small to large sort No. 0 bit is the minimum value the last one is the maximum value4Arr.sort (function(A, b) {5      returnA-B;//Sort by small size6      //return b-a; Sort by size from big to small7 })8 Console.log (arr);9 varMin=arr[0];Ten varMax=arr[arr.length-1]; OneConsole.log (Min,max);//1,456 A  - vararr=[3,2,6,1,45,23,456,23,2,6,3,45,37,89,30]; - //the second method makes comparisons based on each array element by assuming the first largest or smallest and then comparing it with the elements that follow. the varMin=arr[0]; - varMax=arr[0]; -  for(vari=0;i<arr.length;i++){ -arr[i]<min?min=arr[i]:min; +arr[i]>max?max=Arr[i]:max; - } +Console.log (Min,max);//1,456 A  at //The third method is based on the math.max,math.min and the Apply parameter - varmin=Math.min.apply (Window,arr); - varmax=Math.max.apply (Window,arr); - Console.log (Min,max); -  - //The fourth method is based on Math.max,math.min and Evel in //eval (' string ') converts a string into an expression - varMin=eval (' Math.min (' +arr.tostring () + ') '); to varMax=eval (' Math.max (' +arr.tostring () + ') '); +Console.log (Min,max);

To write a function is to find the average of the parameters, which involves arguments the class array and converting the array into arrays and then using the array method to calculate the average

1 //find the average of a set of numbers, remove the minimum and maximum numbers to find the average of an array2 //Use the slice cloning array function on the array prototype chain and call to change this, and sort to remove the maximum and minimum values3 functionAvgfn () {4     varArr=array.prototype.slice.call (arguments);//borrowing slice cloning to convert an array of arguments to a group5Arr.sort (function(A, b) {6         returnA-b;7     })8Arr.shift ();//Remove the first of the array9Arr.pop ();//Remove the end of an arrayTen     return(Eval (arr.join (' + '))/arr.length). toFixed (2); One } AConsole.log (AVGFN (8.0,3.0,5.0,4.0,5.5,6.5,8,4.0,5.5));

Write another way to understand the use of call more and to understand the array conversions

1 functionAvgfn () {2Array.prototype.sort.call (arguments,function(A, b) {3         returnA-b;4     });5 Array.prototype.shift.call (arguments);6 Array.prototype.pop.call (arguments);7     return(Eval (Array.prototype.join.call (arguments, ' + '))/arguments.length). toFixed (2);8 }9Console.log (AVGFN (8.0,3.0,5.0,4.0,5.5,6.5,8,4.0,5.5));

Something to note: in IE6 to IE8, it is compatible with the array conversion arrays of arguments, but there is incompatibility between the array of elements and the set of nodes, so what do you do with a for loop? So how do I go to a compatible page? You can use the try{} catch (e) {} to determine non-compatibility.

Let's talk a little bit about the sort method.

1 vararr=[2,3,45,6,45,67,56,6,56,789,55,45,43,45];2 //Arr.sort (); This method only works for numbers less than 10, for example, it thinks 8>763Arr.sort (function(A, b) {4     returnA-B;//The meaning is small platoon to big use another kind of meaning understanding is return more than 0 is small platoon to big, less than 0 is big row to small5     //if you want to disrupt an array then return Math.random ()-0.5 ok. 6 })7 //OK, it's all about the sort of numbers. If it's a Chinese string, then how to sort it?8 //Then put the artifact. Localecompare () This is the ability to convert a Chinese string into a phonetic string and then compare it to a string less than the parentheses in the preceding string that is return 1 or see the code.9arr=[' Xu Wendong shoes ', ' Blog Park children's Shoes ', ' Zhang San ', ' John Doe '];TenArr.sort (function(A, b) { One     returnA.localecompare (b); A }) -Console.log (arr);//(4) ["Blog Garden Children's Shoes", "Zhang San", "Xu Wendong Shoes", "John Doe"]

To find the minimum and maximum values of an array, the average of a set of numbers, a detailed description of the sort function, an array of classes

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.