How to redefine Map(),Filter() with Reduce()

How do i use filter(), map() and reduce() at the same time on this challenge.
Here is my own code

function getRating(watchList){

  // Only change code below this line

  var averageRating = watchList
  .map(item => {
    return item.imdbRating
  })
  .filter(item => {
    return item.imdbRating
  }) 
  // Only change code above this line
  return averageRating;
}
console.log(getRating(watchList));

Ends Here

ChallengeCode: Use the reduce Method to Analyze Data

Link to the challenge:

Here is the solution. I can see why you got stuck on this one. It took me a little bit to figure out.

function getRating(watchList){
  
  var averageRating =
  watchList
    
    .filter(film => film.Director === "Christopher Nolan")
   
    .map(film => Number(film.imdbRating))
   
    .reduce((sumOfRatings, rating) => sumOfRatings + rating) /
 
  watchList.filter(film => film.Director === "Christopher Nolan").length;

  return averageRating;
}
console.log(getRating(watchList));

It is great that you solved the challenge, @pizzaguy0107, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

@AdamsAlbarka, I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

@ilenia, thank you for your input. I will keep that in mind in the future. :slightly_smiling_face:

With this code you are getting an empty array because map is changing each item of the array to a number, and so when your callback in filter returns item.imdbRating it is always returning undefined, making so that no element of the array is kept

Try to think what conditions you need for the various steps of the challenge

1 Like

I would suggest you forgo the method chaining until you understand what each of the array methods is returning. Run them separately and log out the returning array.