I’m getting a different result I hope you could help me.
var averageRating = watchList.filter((elem) => elem.Director == "Christopher Nolan").reduce((sum, elem,currentIndx,array) => {
sum += Number(elem.imdbRating); // 34.7
return sum / array.length;
}, 0);
// Add your code above this line
console.log(averageRating);
What I get is 2.8062500000000004
which is wrong. I don’t know if it is a good idea to do return sum / array.length
.
But when I tried to test return Number(sum)
on that code it returns 34.7 which is fine. I also tried to change it to return Number(array.length)
which is 4 and that’s fine too. Although when I test it with return sum / array.length
it shows a number but different than expected. Could you please tell more what is exactly happening there? It seems it is not dividing the sum each time because when I tried it on the calculator it is different too.
Oh ok thank you. Now I undestand it.
Had this same problem. Knew why it was wrong, but not how to fix it. This helped! Thanks!