Can someone tell me what is wrong with this code? I am trying to return the largest numbers from four separate arrays.
function largestOfFour(arr) {
var longList = [];
for (var i in arr){
var longest = 0;
for (var x=0; x<i.length; x++){
if (i[x] > longest){
longest=i[x];
}
longList.push(longest);
}
}
return longList;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
EDIT: Lye Fish provided the answer below. Here is the new code:
function largestOfFour(arr) {
return arr.map(Function.apply.bind(Math.max, Math));
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
for(var k in ...)for an array. It does not do what you think it does. Btw, usually people would use the variable namekorkeyto indicate that it gives you the key instead of the value as in some other languages like Python.your_array.map(Function.apply.bind(Math.max, Math))