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);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
Answer from Roatin Marth on Stack OverflowVideos
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);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
var max_of_array = Math.max.apply(Math, array);
For a full discussion see: http://aaroncrane.co.uk/2008/11/javascript_max_api/
I realized the answer as I was posting my own question: This is the most succinct way of taking the min of an array x in JavaScript. The first argument is totally arbitrary; I find the 0 confusing because the code intuitively means "Take the min of 0 and x," which is absolutely not the case. Using the Math object makes more sense for human-readability, but the Raphael.js authors are obsessed with minification and 0 is three bytes shorter.
See http://ejohn.org/blog/fast-javascript-maxmin/
For readability's sake, I'd strongly urge people to stop doing this and instead define a function along the lines of
function arrayMin(arr) { return Math.min.apply(Math, arr); };
The reason is this:
Your input
xis an arrayThe signature of
Math.min()doesn't take arrays, only comma separated argumentsIf you were using
Function.prototype.call()it would have almost the same signature, except the first argument is thethiscontext, i.e. who's "calling"- Example:
Math.min.call(context, num1, num2, num3)
- Example:
The
contextonly matters when you refer tothisinside the function, e.g. most methods you can call on an array:Array.prototype.<method>would refer tothis(the array to the left of the 'dot') inside the method.Function.prototype.apply()is very similar to.call(), only that instead of taking comma-separated arguments, it now takes an array after thecontext.Function.prototype.call(context, arg1, arg2, arg3)Function.prototype.apply(context, [arg1, arg2, arg3])
The
0ornullthat you put in as the first argument is just a place shifter.
These function expect just two arguments. If you want the minimum of an array you can use IntStream.
int[] a = { 1, 5, 6 };
int max = IntStream.of(a).max().orElse(Integer.MIN_VALUE);
int min = IntStream.of(a).min().orElse(Integer.MAX_VALUE);
You can simply used in-build java Collection and Arrays to sort out this problem. You just need to import them and use it.
Please check below code.
import java.util.Arrays;
import java.util.Collections;
public class getMinNMax {
public static void main(String[] args) {
Integer[] num = { 2, 11, 55, 99 };
int min = Collections.min(Arrays.asList(num));
int max = Collections.max(Arrays.asList(num));
System.out.println("Minimum number of array is : " + min);
System.out.println("Maximum number of array is : " + max);
}
}
Edit: I am not asking for a way to use these methods with arrays. What I am asking is why in the first place do they not accept arrays.
However, I will list the solutions mentioned:
Math.max.apply(null, arr)Math.max(...arr)arr.reduce((max, current) => (current > max ? current : max), -Infinity);