The goal is to return the sum of the numbers in the array using the forEach method. What am I doing wrong?
list = [6,7,1,3,1,17,4,12,1,5,0,13,15]
function totalPoint(array) {
let sum = 0;
array.forEach(function(number){
sum += number
return sum
})
}
totalPoint(list)
It should have the same result as this:
function totalPoints(array){
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i]
}
return sum
}
reduceit is easier.array.reduce((total, curr) => total+=curr, 0);reduceis when you wanted an accumulated result from the array like sum, product, string concat with a filter and what not! in this use case definitely is easier as you don't have to maintain any variable.returncounts for. So far, i assumed it's just people not really trying, but by the sheer amount of issues, i may have to reconsider. The problem is solely, that yourreturnis for the callback of theforEach, and yourtotalPointdoesn't have anyreturn. Simply move thereturnto the end oftotalPoint.