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?

Your code so far

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));

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-reduce-method-to-analyze-data`Preformatted text`

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 can’t figure out how to console.log inside the reduce function

Correct. Do you see how your second solution is doing that and your first solution is dividing each number by five before adding them together? Well, it’s actually adding the number to the previous total and then dividing by the total amount of numbers. The point here is that it is dividing by the total amount of numbers each time instead of waiting until the end when you have totaled all of the numbers.

I would take @camperextraordinaire’s suggestion and add some console.logs 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.

This explanation makes much more sense, thank you very much.

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