Problem understandint reduce method logic

Tell us what’s happening:
Hello,

I don’t understand why I need to do this:

 var averageRating = toNumbers.reduce(function (total, cur, index, arr){
  total += cur; 
  if (index === arr.length - 1){
    return total/arr.length
  } else {
    return total;
  }
 }, 0);

Instead of this:

 var averageRating = toNumbers.reduce(function (total, cur){
  return (total += cur) / 4;
 }, 0);

to get the correct result. Thanks!

Your code so far


function getRating(watchList){
 // Only change code below this line
 let nolanMovies = watchList.filter(function(movie){
   return movie.Director === "Christopher Nolan" 
 })

 let allRatings = nolanMovies.map(function(rating){
   return rating.imdbRating;
 })

 let toNumbers = allRatings.map(function (number){
   return parseFloat(number);
 })
 console.log(toNumbers)

 //this code doesn't return the correct result
 var averageRating = toNumbers.reduce(function (total, cur){
  return (total += cur) / 4;
 }, 0);
 
//this code returns the correct result
 var averageRating = toNumbers.reduce(function (total, cur, index, arr){
  total += cur; 
  if (index === arr.length - 1){
    return total/arr.length
  } else {
    return total;
  }
 }, 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; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

For every element of the toNumbers array, you are dividing the new total by 4. Instead, you need to divided the total by 4 only after all the numbers have been summed.

On a side note, I recommend not using a hard coded value of 4, but instead calculate this number from the data given. This way, if a different array of movies was passed to the function it would still provide the correct result.