Tell us what’s happening:
When validating the challenge, it prints error : Your code should use the reduce method.
Your code so far
function getRating(watchList){
// Only change code below this line
const averageRating = watchList.filter((titre) => titre["Director"] == "Christopher Nolan")
.map((titre) => Number(titre["imdbRating"]))
.reduce((sum, rank) => sum + rank)
/
watchList.filter((titre) => titre["Director"] == "Christopher Nolan")
.length;
// Only change code above this line
return averageRating;
}
NOTE : same error occurs using copy/paste from “Get Hint” solutions 1. The 2nd & 3th works nice.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
.
Challenge: Use the reduce Method to Analyze Data
Link to the challenge:
So what exactly do you need to help with?
I think you’re supposed to use only and only reduce method not others as filter and map
sanity
May 8, 2021, 10:08am
#3
There’s an error affecting this challenge, to make it pass just put all chained methods (.reduce
, .map
, etc.) on the same line after the watchList
.
For more information see: Bug with challenge: Use the filter Method to Extract Data from an Array · Issue #41951 · freeCodeCamp/freeCodeCamp · GitHub
Thank both of you for the quick answers.
I should have checked first on GitHub before posting on the forum for “help”.
Have a nice day & happy coding
SPOILER ALERT
Solution using only reduce method:
function getRating(watchList){
// Only change code below this line
var averageRating;
averageRating = watchList.reduce((ratings, movie) => {
if (movie["Director"] === "Christopher Nolan") {
return ratings.concat(Number(movie["imdbRating"]));
}
return ratings
}, [])
.reduce((average, rating, i, arr) => {
if (i === arr.length - 1) {
return (average + rating) / arr.length;
}
return average + rating;
});
// Only change code above this line
return averageRating;
}
1 Like
system
closed
November 6, 2021, 10:29pm
#6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.