Recently, I encountered a challenge on freecodecamp
I managed to write the code successfully, but unfortunately, my browser crashed and I lost all the progress. When I attempted to rewrite the code, it was returning an incorrect answer (a value of 2).
I'm seeking assistance to understand why this is happening without directly giving me the solution. Can you provide insight into what might be causing this issue and how it can be fixed? That's all I require.
Below is the code snippet in question:
// the global variable
var watchList = [{
"Title": "Inception",
"Year": "2010",
...
// Add your code below this line
var arr = [];
watchList.map(item => {
if (item.Director === "Christopher Nolan") {
arr.push(Number(item.imdbRating));
}
});
var averageRating = arr.reduce((before, after) => {
return (before + after) / arr.length;
});
// Add your code above this line
console.log(averageRating);
If this question doesn't belong here or if there are any errors in my approach, please let me know so I can correct them accordingly.