-4

I have an array of numbers. How do I find the highest and the lowest value in that array? What is the simplest solution?

This seems to be the simplest answer to my question:

var arr = [1,2,4]
var min = Math.min(...arr)
var max = Math.max(...arr)

It's based on these answers.

BlueSkies
  • 109
  • 8

2 Answers2

0

How about augmenting the built-in Array object to use Math.max/Math.min instead:

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};
0

I use this function that display the max and min of an Array

    function arrMinMax(arr){
    const min = arr.reduce((a, b) => Math.min(a, b))
    const max = arr.reduce((a, b) => Math.max(a, b))

    console.log("Array : ["+ arr +"]\nMin : "+min +"\nMax : " +max)
}