Use the array argument in reduce to calculate the mean value


I was doing this lesson about Functional Programming in Javascript :

In the solution, it is written I have to recalculate how many movies are in the source array with Director : "Christopher Nolan" in order to calculate the average rating of theses movies.

It has come to my attention that the callback function of the reduce method has an optional array argument, so I wanted to use it to make the code leaner.

function getRating(watchList){
  // Only change code below this line
  var averageRating = watchList
    .filter(element => element.Director == "Christopher Nolan")
    .map(element => parseFloat(element.imdbRating))
    .reduce((sum, val, index, array) => (sum+val)/array.length,0);
  // Only change code above this line
  return averageRating;

Unfortunately, it doesn’t work. Is it because the array that I’m trying to use correspond to watchList, or maybe have I made a mistake ?

Thank you in advance for your help !


reduce is an array method with works by iterating over each element over the array so in that “on each iteration, find the array.length and then divide it by the sum of the previous and the current rating”

so basically you need to find the average only once after youre done summing up the whole thing with reduce.

Hope this helped! :slightly_smiling_face:

@staranbeer, you misunderstood my problem. I already know what this method is doing, but I want to understand why I can’t use the array argument of the callback function.

In the doc from Mozilla :

A function to execute on each element in the array (except for the first, if no initialValue is supplied).
It takes four arguments :
The accumulator accumulates callbackFn’s return values. It is the accumulated value previously returned in the last invocation of the callback—or initialValue , if it was supplied (see below).
The current element being processed in the array.
index (Optional)
The index of the current element being processed in the array. Starts from index 0 if an initialValue is provided. Otherwise, it starts from index 1 .
array (Optional)
The array reduce() was called upon.

This is precisely the array I’m interested in to put in use for my codebase.

The array parameter is working and you can see that it’s working if you do this.

.reduce((sum, val, index, array) => console.log(array);

the output is

[ 8.8, 8.6, 9, 8.3 ]
[ 8.8, 8.6, 9, 8.3 ]
[ 8.8, 8.6, 9, 8.3 ]

but if it isn’t working in a particular situation then posting this the specific problem would help us to debug it.

I see, so even if array.lenght has the value I’m looking for, I just can’t use my previous function :

.reduce((sum, val, index, array) => (sum+val)/array.length,0);

because it’s just mathematically not corresponding to what I want (it returns 8.8/4 + (8.8/4 +8.6)/4 … ).

I guess I’ll just stick to the solution then.

Thanks for helping me understand this !

yes because it would simply do the same thing over and over again for the each value in the array which is not what you want in this case. Glad i could help! :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.