I'm currently facing a challenge where I need to calculate the total sum of an array using the .reduce() method within a function. Instead of simply returning the sum, I am required to call a callback function and pass the sum as an argument.
Though I believe I am using the .reduce() method correctly, I keep receiving 'undefined' as a result. I'm struggling to understand how I can effectively invoke a function within another function.
Any assistance or explanations on this matter would be greatly welcomed!
Question: Can you sum all integers in the numbers array and then call a callback function with the sum as an argument?
Here is my code:
function sumArray(numbers, cb) {
// Insert code here
const result = numbers.reduce(function(sum, currentValue) {
return sum + currentValue;
}, 0);
cb([1, 2, 3, 4, 5], result);
}
function addTwo(num) {
const result = num + 2;
}