The function returns the correct result but it doesn’t pass the Your code should return the correct output after modifying the `watchList` object. condition.
function getRating(watchList) {
// Only change code below this line
let nums = []
let averageRating = watchList.map(function(a) {
if (a.Director === 'Christopher Nolan') {
return a.imdbRating
}
}).forEach(str => {
nums.push(Number(str))
})
nums = nums.slice(0, 4)
return nums.reduce((a, b) => a + b) / nums.length
// Only change code above this line
}
console.log(getRating(watchList));
You do not need a separate nums array. map makes an array. Make your map + filter make what you need instead of abandoning the result of the map… You never use the averageRating variable. That’s an indicator that something is strange.
function getRating(watchList) {
// Only change code below this line
let averageRating = watchList.map(function(a) {
if (a.Director === 'Christopher Nolan') {
return Number(a.imdbRating)
}
})
averageRating = averageRating.filter(a => Boolean(a))
return averageRating.reduce((a, b) => a + b) / averageRating.length
// Only change code above this line
}
That’s what I was trying to get at. You cn arrange that logic in a few ways - I think it’s worth looking at the possible solutions listed with the hints for this challenge.