Hello, I’m asked to get average rating of Christopher Nolan movies using map/filter and reduce, i’ve got filter part done(commented out since the complete ‘sentence’ didn’t work), but now i’ve no idea how to reference the filtered array in reduce so that i could divide averageRating by its length.
function getRating(watchList) {
// Only change code below this line
let averageRating;
//console.log(watchList.filter((movie) => movie["Director"] === "Christopher Nolan")
console.log(watchList.reduce((averageRating, movie) => averageRating + parseInt(movie["imdbRating"]), 0) / watchList.length)
// Only change code above this line
return averageRating;
}
getRating(watchList);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Functional Programming - Use the reduce Method to Analyze Data
You have used array.filter() on line before this. But the original array is NOT modified. You can console.log it out to see.
So you need to store your filter result to a variable and proceed from there.
parseInt(movie[“imdbRating”])
if you convert string to integer with parseInt, the number behind decimal point will be lost. You can use Number() instead.
There was another similar-ish about using map and filter, wasn’t a problem there iirc.
oh well, task solved:
function getRating(watchList) {
// Only change code below this line
let averageRating;
let nolanArr = watchList.filter((movie) => movie["Director"] === "Christopher Nolan")
averageRating = nolanArr.reduce((averageRating, movie) => averageRating + Number(movie["imdbRating"]), 0) / nolanArr.length;
// Only change code above this line
return averageRating;
}
getRating(watchList);