Single Array
Completed Code
var arr= [ 68, 47, 85, 22];
console.log(Math.max.apply(null, arr));Explanation
We use a new function here: Math.max.apply(null, arr). Let's break each part down:
Math - an object that allows us to do math stuff like rounding, getting the square root, trigonometry, and exponentiation.
.max - returns the maximum, the number with the highest value.
.apply - JavaScript arrays don't have a
max()method so you have to apply it. Otherwise it would outputNaNor Not a Number.(null, arr) - the first argument of
apply()sets the value ofthis. We don't have athisin our function so just set it tonull.arris just our array variable.
Smallest
If you want to calculate the smallest number in an array, just replace .max() with .min() to return the minimum.
var arr= [ 68, 47, 85, 22];
console.log(Math.min.apply(null, arr));Last updated