【发布时间】:2015-10-30 06:41:51
【问题描述】:
function largestOfFour(arr) {
var newArray = [];
for(var i =0; i <=arr.length-1; i++){
console.log(arr[i]);
newArray[i] = Math.max(arr[i]);
}
console.log(newArray);
// You can do this!
return newArray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
所以我不明白为什么我得到NaN,即使我在我的math.max 函数中提供了一个参数。在我的 for 循环中的 console.log 中,我让主数组中的每个数组都显示出来。这意味着如果我在 max 函数中使用相同的arr[i],它应该给我该子数组的最大值。
【问题讨论】:
-
你想在数组中找到最大的吗?
-
yes 所以我想要完成的是遍历每个子数组并找到其中的最大整数,并将最大整数传递到一个单独的数组中。所以函数返回一个包含 4 个整数的数组,每个整数是主数组中 4 个子数组的最大整数。
标签: javascript