How to return max element of a given array using the code structure below?
var array = [100, 0, 50];
function max(array) {
// code
}
Thank You.
How to return max element of a given array using the code structure below?
var array = [100, 0, 50];
function max(array) {
// code
}
Thank You.
Try Math.max.apply(Math, array)
var array = [100, 0, 50];
Another solution
function max(array) {
var max = array[0];
for (var i = 0; i < array.length-1; i++) {
max = max > array[i+1] ? max : array[i+1];
}
return max;
}
Use the spread operator (ES2015):
Math.max(...arr)
The spread operator is not yet available in all browsers and runtimes - check the compatibility table.