Functional Programming - Use the reduce Method to Analyze Data

Tell us what’s happening:
I was able to solve the problem but I have a question about the reduce method. Whenever I tried to divide in the reduce method like this.

let averageArray = mapArray.reduce((d, m) => (d + m) / averageArray.length)

I kept getting 2.9 instead of 8.625. However, once I created the result variable and just used the variables to divide it worked. Why does this happen?

``````function getRating(watchList) {
// Only change code below this line
let averageRating = watchList.filter(director => director.Director === "Christopher Nolan")
console.log(averageRating.length);
let mapArray = averageRating.map(movie => parseFloat(movie.imdbRating))
let averageArray = mapArray.reduce((d, m) => (d + m))
let result = averageArray / averageRating.length
// Only change code above this line
return result;
}

console.log(getRating(watchList));

``````

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

Challenge: Functional Programming - Use the reduce Method to Analyze Data

How do you find the average of a set of numbers? Explain this in plain language and then tell us which version is implementing your explanation. And remember, `reduce` is running the callback on every number passed into it at the time it is passed into it.

Well I want to take the mean which is why I did (d +m) / averageRating. I do not understand every number being passed into it at callback in the sense that this wouldn’t work.

Technically, you want to find the average score. You explained that as `(d+m)/averageRating`. I want you to explain it in plain English instead. No code. If I gave you five numbers, how do you find the average of those five numbers?

You would add those 5 numbers together and then divide by 5.

I would take @camperextraordinaire’s suggestion and add some `console.log`s in the callback so you can see what the values are that are being passed into it.
To add logging you need to use curly braces around the function body so you can add more than one statement. But when you do this you will need to add an actual `return` statement.