Skip to content Skip to sidebar Skip to footer

How To Use Math.max And Max.min Functions In An Array?

I tried this method in an array to get min and max values but it doesn't work. var array = [3, 6, 1, 5, 0, -2, 3]; var min = Math.min( array ); var max = Math.max( array ); docum

Solution 1:

Use Function.apply

min = Math.min.apply(null, array);min = Math.max.apply(null, array);

apply is very similar to call(), except for the type of arguments it supports. You can use an arguments array instead of a named set of parameters. With apply, you can use an array literal

Solution 2:

Try this.

functionminmax(){
  var max = Math.max.apply(Math, arguments);
  var min = Math.min.apply(Math, arguments);
  console.log(max);  // print 6console.log(min); // print -2
}
minmax (3, 6, 1, 5, 0, -2, 3);

Post a Comment for "How To Use Math.max And Max.min Functions In An Array?"