javascript - What is the most efficient way to get the highest and the lowest values in a Array -


Is there something like PHP: max () in javascript?

Say I have such an array:

  [2, 3, 23, 2, 2345, 4, 86, 8, 231, 75]  

And I want to return the highest and lowest value to this array.

function madmax (arr) {var max = arr [0], min = arr [1] (if (min> max) {max = arr [1] min = arr [ 0]} for (i = 1; i & lt; = arr.length; i ++) {if (arr [i] & gt; max) {max = arrivals [i]] and if (arr [i] & Lt; min) {min = arr [i]}} Return Maximum, Minimum} Madame ([123, 2, 345, 153, 5, 98, 456, 4323456, 1), 234, 1974, 238, 4 ])

Is it easy to retrieve the maximum and minimum values ​​of the array?

and the built-in ways can be really fast:

  Array = [2, 3, 23, 2, 2345, 4, 86, 8, 231, 75]; Var max = Math.max.apply (Math, Array); // 2345 var min = Math.min.apply (Math, Array); // 2  

Comments