There is a code that accepts an array and must return a sorted array of two elements, the first is the smallest, the second is the largest value in the transmitted array. Logically did everything right, but I don’t make the code work. Help me to understand.

function minMax(arr){ function determineOrder(num){ return (num[0] > num[1]) ? array.push(num[0], num[1]) : array.push(num[1], num[0]) ; } return arr.map(determineOrder); } minMaxminMax([1,2,3,4,5]) ; 

And I expected this result:

 minMax([1,2,3,4,5]) == [1,5] minMax([2334454,5]) == [5, 2334454] minMax([1]) == [1, 1] 
  • I get some strange> Uncaught ReferenceError: minMaxminMax is not defined. Where does minMaxminMax come from? - spectre_it
  • 2
    from this line: minMaxminMax([1,2,3,4,5]) ; - Grundy
  • 2
    Well, the function itself does not understand that :) - Grundy
  • one
    es6: const minmax = (a, m = Math) => [m.min(...a), m.max(...a)]; With the tip of Grundy - saaaaaaaaasha

2 answers 2

Without any cleverness. Inside the function, we first define two variables at a minimum and a maximum equal to the initial element of the array. Then we compare them with all the elements of the array in order.

 function minMax(arr){ var min = arr[0], max = arr[0]; arr.forEach(function(val) { min = Math.min(min, val); max = Math.max(max, val); }); return [min, max]; } console.log(minMax([1,2,3,4,5])); console.log(minMax([2334454,5])); console.log(minMax([1])); 

    Three implementations return the smallest and largest values ​​from the array.

    es5:

     function minMax(arr){ return [Math.min.apply(Math, arr), Math.max.apply(Math, arr)]; } console.log(minMax([1,2,3,4,5])); console.log(minMax([2334454,5])); console.log(minMax([1])); 

    es6:

      function minMax(arr){ return [Math.min(...arr), Math.max(...arr)]; } console.log(minMax([1,2,3,4,5])); console.log(minMax([2334454,5])); console.log(minMax([1])); 

    old way:

     function minMax(arr) { var a = arr.sort(function(a,b) {return ab}); return [a[0], a[a.length-1]]; } console.log(minMax([1,2,3,4,5])); console.log(minMax([2334454,5])); console.log(minMax([1])); 

    • one
      in the new standard everything is simpler [Math.min(...arr), Math.max(...arr)] . But this code goes through the array twice, and in the next answer - once - Grundy
    • @Grundy, thanks for the valuable comments. Tell me, why is passing through the array twice a more expensive operation than double matching all the elements? min = Math.min (min, val); max = Math.max (max, val); - spectre_it
    • Actually, I did not say that it is more expensive. It all depends on the native implementation. And the possible optimization of the generated code. Just say that a particular approach will always be more expensive, in all browsers - it is impossible - Grundy
    • I wonder why you personally prefer the code written by the respected Mr. Visman? - spectre_it
    • one
      For example, because it is the most obvious option. Only, instead of forEach, you could use for to remove the callback call. But here as someone more like it - Grundy