Single Array
Completed Code
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 outputNaN
or Not a Number.(null, arr) - the first argument of
apply()
sets the value ofthis
. We don't have athis
in our function so just set it tonull
.arr
is 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.
Last updated