So I am building a piece of JavaScript code that returns an array of the largest numbers from each of the provided sub-arrays.
function largestOfFour(arr) {
var array = [];
for (i = 0; i < arr.length; i++) {
// return arr[i].join() on this line gives 4,5,1,3
array.push(Math.max(arr[i].join()));
}
return array;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
My question is why the:
Math.max(arr[i].join())
returns a null value
?
If I return
arr[i].join()
on the previous line (as in code comment) it returns to me 4,5,1,3
for the 1st iteration. If I put in Math.max(4,5,1,3)
it returns 5
which is what I want.
Assigning the arr[i].join()
to a variable and then putting in Math.max
also returns null
. I would run theMath.max
through a reduce function, but it won't let me run a function through a loop.
I'm sure there is a simple reason why this doesn't work, but I can't find an explanation anywhere. I don't need help with a solution to the overall problem - just help with understanding why the Math.max
won't work.