我想解决一个问题,我必须在函数内使用.reduce()方法获得数组的总和,而不是返回总和,我需要调用回调并在那里传递总和。获取数组的另一个函数和回调函数
我很确定我正确使用.reduce(),但我一直在'未定义',对于我的生活,我无法弄清楚如何调用函数内的函数。
任何帮助/解释将不胜感激!
问题:总结numbers数组中的所有整数。 而不是返回sum调用cb并在那里传递总和。
我的代码:
function sumArray(numbers, cb) {
//code here
numbers.reduce(function(sum, currentValue) {
return sum + currentValue;
}, 0);
cb([1, 2, 3, 4, 5], result);
}
function addTwo(num) {
const result = num + 2;
}
你在哪里,甚至调用'sumArray()'? – charlietfl