Reduce solution explained

  1. Can you explain what the sum equals specifically? (It seems it’s almost always sum so far so I got the solution, but I’m not understanding what is being added to the ratings)
  2. Why was 0 needed?
  3. Am I correct in my understanding of the other arguments below?
  • accumulator is sum
  • currentValue is movie
  • index is not needed
  • array is not needed
  • initialValue is 0

Your code so far

function getRating(watchList){
  // filters movies for those directed by Christopher Nolan
  var cNolan = watchList.filter(movie => movie["Director"] == "Christopher Nolan")
  // uses reduce to get the average IMDB rating
  var averageRating = cNolan.reduce((sum,movie) => {
      return sum + parseFloat(movie.imdbRating);
    }, 0) / cNolan.length;

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

Might be helpful:

2 Likes

As you pointed out, the sum is the accumulator, so it starts at the initial value 0 and then you add to it as you go through each item in the array. It maintains its value through each iteration. So if for the first item in the array you add 5 to sum, then sum equals 5 when you get to the second item in the array. In this case you are adding the imdbRating to sum for each movie in the array.

I know you are not a fan of the MDN docs, but this is what it says for the initialValue:

" A value to use as the first argument to the first call of the callback . If no initialValue is supplied, the first element in the array will be used as the initial accumulator…"

The first element in the array is an object and you want sum to be a number, so it wouldn’t make sense to use an object as the initial value and thus you have to explicitly set the initial value to 0.

2 Likes

What might be confusing you is that you’re missing the power of what reduce is. It’s not just for summing numbers which is why the initial value is needed. Reduce makes no assumptions about what you intent to do.

Reduce can be used to replace absolutely any array method. You can map, filter, sum, concat, find etc.

The “accumulator” is just that. An accumulator. Calling it “sum” confuses what it’s intended for. It’s accumulating a value that can be absolutely anything. Sure it could be a running sum of numbers but it could also be an object that you’re building up or another array, or an array of objects.

So I wouldn’t associate “accumulator” with “sum” personally.

Another way to think of this is that accumulator initially is whatever you pass as the initial value, which could be an object or anything else that you want.

The next value of accumulator is whatever you return from the callback which also can be whatever you want.

Hopefully I explained that right :slight_smile:

3 Likes