我正在研究freecodecamp挑战,并且想知道为什么我的代码不工作以及如何纠正它。在map()中对数组array()使用reduce()
目标是“返回由每个提供的子数组中最大数组成的数组”。
我的尝试是采用降低为地图功能的输入阵图:
function largestOfFour(arr) {
arr = arr.map(function(innerArray){
innerArray = innerArray.reduce(function(previousValue,currentValue){
return currentValue > previousValue ? currentValue : previousValue;
});
});
return arr;
}
console.log(largestOfFour([[4, 5, 1, 3],[1, 2, 3, 4]]));
目前的输出是:[undefined, undefined]
我应该如何修复我的代码?
你分配给'innerArray',但不返回任何东西。 –