Tell us what’s happening:
Hello,
I don’t understand why I need to do this:
var averageRating = toNumbers.reduce(function (total, cur, index, arr){
total += cur;
if (index === arr.length - 1){
return total/arr.length
} else {
return total;
}
}, 0);
Instead of this:
var averageRating = toNumbers.reduce(function (total, cur){
return (total += cur) / 4;
}, 0);
to get the correct result. Thanks!
Your code so far
function getRating(watchList){
// Only change code below this line
let nolanMovies = watchList.filter(function(movie){
return movie.Director === "Christopher Nolan"
})
let allRatings = nolanMovies.map(function(rating){
return rating.imdbRating;
})
let toNumbers = allRatings.map(function (number){
return parseFloat(number);
})
console.log(toNumbers)
//this code doesn't return the correct result
var averageRating = toNumbers.reduce(function (total, cur){
return (total += cur) / 4;
}, 0);
//this code returns the correct result
var averageRating = toNumbers.reduce(function (total, cur, index, arr){
total += cur;
if (index === arr.length - 1){
return total/arr.length
} else {
return total;
}
}, 0);
// Only change code above this line
return averageRating;
}
console.log(getRating(watchList));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0
.
Challenge: Use the reduce Method to Analyze Data
Link to the challenge: