Use the reduce Method to Analyze Data Solution Change

The challenge

I believe a much more simplified solution would help people understand the code better. After I finish my challenges I always end up checking the solution and most of the time I see unnecessary one liners. This challenge is a great example. Also, the solution provided does not respect DRY

Solutions shouldn’t be show off code.
They should provide a guide on how to achieve results.

I don’t know much but I believe it could be something like this:

/*

  • We start by using .filter to find the Director.
    then we use .map to list the ratings, note we use Number in order to convert the ratings to numbers.
    Finally we use .reduce to find the average of our ratings.

*/

let ratingsList = watchList
  .filter(x => x.Director === 'Christopher Nolan')
  .map(x => Number(x.imdbRating));
let averageRating = ratingsList
  .reduce((a, b) => a + b) / ratingsList.length;

Thank you for all your work and dedication

The solutions in the Guide are all written by community members. It’s super easy to make changes to the guide and submit a a Pull Request. It’s a great way to give back to the community.

1 Like