Reduce Method help!

The last test won’t pass Your code should return correct output after modifying the watchList object. I cannot determine the root of this issue but after some research it may be because I am not using the whole watchList object at some point? I am getting the answer I want but the test is in the way. Thanks!

// The global variable
var watchList = [
  {
    "Title": "Inception",
    "Year": "2010",
    "Rated": "PG-13",
    "Released": "16 Jul 2010",
    "Runtime": "148 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Christopher Nolan",
    "Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
    "Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
    "Language": "English, Japanese, French",
    "Country": "USA, UK",
    "Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.8",
    "imdbVotes": "1,446,708",
    "imdbID": "tt1375666",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Interstellar",
    "Year": "2014",
    "Rated": "PG-13",
    "Released": "07 Nov 2014",
    "Runtime": "169 min",
    "Genre": "Adventure, Drama, Sci-Fi",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan, Christopher Nolan",
    "Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
    "Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
    "Language": "English",
    "Country": "USA, UK",
    "Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
    "Metascore": "74",
    "imdbRating": "8.6",
    "imdbVotes": "910,366",
    "imdbID": "tt0816692",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "The Dark Knight",
    "Year": "2008",
    "Rated": "PG-13",
    "Released": "18 Jul 2008",
    "Runtime": "152 min",
    "Genre": "Action, Adventure, Crime",
    "Director": "Christopher Nolan",
    "Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
    "Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
    "Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
    "Language": "English, Mandarin",
    "Country": "USA, UK",
    "Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
    "Metascore": "82",
    "imdbRating": "9.0",
    "imdbVotes": "1,652,832",
    "imdbID": "tt0468569",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Batman Begins",
    "Year": "2005",
    "Rated": "PG-13",
    "Released": "15 Jun 2005",
    "Runtime": "140 min",
    "Genre": "Action, Adventure",
    "Director": "Christopher Nolan",
    "Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
    "Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
    "Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
    "Language": "English, Urdu, Mandarin",
    "Country": "USA, UK",
    "Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
    "Metascore": "70",
    "imdbRating": "8.3",
    "imdbVotes": "972,584",
    "imdbID": "tt0372784",
    "Type": "movie",
    "Response": "True"
  },
  {
    "Title": "Avatar",
    "Year": "2009",
    "Rated": "PG-13",
    "Released": "18 Dec 2009",
    "Runtime": "162 min",
    "Genre": "Action, Adventure, Fantasy",
    "Director": "James Cameron",
    "Writer": "James Cameron",
    "Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
    "Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
    "Language": "English, Spanish",
    "Country": "USA, UK",
    "Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
    "Metascore": "83",
    "imdbRating": "7.9",
    "imdbVotes": "876,575",
    "imdbID": "tt0499549",
    "Type": "movie",
    "Response": "True"
  }
];

function getRating(watchList){
  // Only change code below this line
  
  var averageRating=watchList.filter(n=>n.imdbRating>7.9)
  averageRating=averageRating.map(n=>n.imdbRating)
  averageRating=averageRating.map(Number)
  averageRating=averageRating.reduce((a,b)=>a+b/4,0)

  

  // Only change code above this line
  return averageRating;
}
console.log(getRating(watchList));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

First of all, don’t use var in this context. Var creates a global variable, which you don’t need hardly ever. Ideally you should usually be using let or const almost all the time.

However, the challenge started you out with the var declaration so it’s fine for now.

Secondly, you’re supposed to write a reduce function. You shouldn’t need anything else. Inside getRating, write a single reduce function that takes watchList as an argument, and the function itself is returned.

Like so:

function getRating(watchList) {
   var averageRating = watchList.reduce((x) => {
      javascript stuff;
   });
   return averageRating;
}

So there is no way to change what I have posted to pass the challenge? I have tried using just reduce but the examples in the challenge are pointless, the example you have given uses one x argument while the example in the challenge has 2. I have to look for a specific director who is “Christopher Nolan”, then I have to get his ratings, then get the sum of those ratings, divide them by four and return the answer all using reduce?? Where are the lessons on those? I researched different ways online and this challenge is so specific it’s led no where. I’m gonna stick to just trying to make this code work if possible

Hello!

What @windowpuncher wrote is correct, but just one way to do it (it’s simpler). You can keep your code, you’re just not writing it right.

For instance:

var averageRating = watchList.filter(n=>n.imdbRating>7.9)

What do you think that line does? Maybe if you explain it to us you’ll see the problem :slight_smile:.

On the other hand, the average formula is not right. The average is the sum of all values (reduce) divided by the length of the data:

avg = moviesDirectedByNolanSum / moviesDirectedByNolan.length;
2 Likes

The line I wrote for the filter searches for any movie over 7.9. which is what I want because Christopher Nolan movies are all over 7.9. I used this way of searching because I couldn’t figure out how to search for his name.

I see… in this exercise, the data is fixed (won’t change), but in a real case scenario it would vary, so that’s not a reliable way to do it.

To filter by the director name, simply change imdbRating > 7.9 with Director === 'Christopher Nolan'.

1 Like

That works! I tried using the same line but it never worked. Maybe because I was not using it in the filter. And the average formula you wrote, do I use that in the reduce or as a regular statement? Reduce is the main thing I am having some trouble understanding. I know that it take two basic arguments and the first one is the count. The second one acts like an [i] in a loop, and goes through each item and update the count. But reduce seems more complicated than that

To apply the formula for the average value (sum of every value / quantity of values), you need to know two things:

  1. The number of elements (filtered array).
  2. The sum of the elements (reduced array).

Once you have already filtered the results, you just need to reduce the values and divide the result by the length of the filtered results. You will need to convert the imdbRating to float (what you’ve already done here: averageRating=averageRating.map(Number)).

1 Like

I passed! I saved each method into it’s own variable to use it the the formula. Because my previous code just rewrote the values of averageRating over and over again. I appreciate the feedback and will look into the reduce method and study some more. But why was my old solution not working properly? I am still kind of confused. Here is the passing code:

function getRating(watchList){
  // Only change code below this line


  let newAverageRating=watchList.filter(n=>n.Director==="Christopher Nolan")
  let newAverageRating1=newAverageRating.map(n=>n.imdbRating)
  let newAverageRating2=newAverageRating1.map(Number)
  let newAverageRating3=newAverageRating2.reduce((a,b)=>a+b,0)
  let averageRating= newAverageRating3/newAverageRating.length



  // Only change code above this line
  return averageRating;
}
console.log(getRating(watchList));

Your old solution was almost done:

var averageRating=watchList.filter(n=> n.Director === 'Christopher Nolan');
averageRating=averageRating.map(n=>n.imdbRating)
averageRating=averageRating.map(Number)
// Here you were dividing the value by 4, which was wrong
// And you weren't dividing the sum by the array length
averageRating=averageRating.reduce((a, b) =>a + b, 0) / averageRating.length;
1 Like

So I have to reference the original array for the lengths, I cannot just manually do the calculation right? If that is the problem then I will keep it in mind for the next time I do a challenge like this.

It’s not the original array.

By the line where you finally calculate the average (reduce), the averageRating has been already filtered and twice transformed by map. You need the length of the filtered array. If you used the original array, you would end up with a different result :slight_smile: (try it out).

1 Like