Reduce challenge

Hi guys,why are my not passing the challenge?
Tell us what’s happening:

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

Can you please share your code? It looks like freeCodeCamp wasn’t able to automatically copy it to the forum.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You still haven’t shared your code.

**Tell us what's happening:**



**Your code so far**

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the line above and the line below,
because they allow your code to properly format in the post.

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0</code>

**Challenge:**  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
**Tell us what's happening:**


function getRating(watchList) {
  // Only change code below this line
  let averageRating = watchList.reduce((movies) => {
    movies["imdbRating"]
  });


  // Only change code above this line
  return averageRating;
}

console.log(getRating(watchList));

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Use the reduce Method to Analyze Data

Link to the challenge:

please follow these instructions for sharing your code

A reduce callback function should have a return value. The line movies["imdbRating"] alone doesn’t perform any logic.

I also don’t see where the variable movies is defined, so you are probably generating a syntax error.

function getRating(watchList) {

  // Only change code below this line

  let averageRating = watchList.reduce((getRating) => {

   getRating["imdbRating"]

    return getRating;

  });

getRating is the name of your function but getRating["imdbRating"] treats it like an array and return getRating treats it like a variable.

Hi @columbuschidozie1 !

I would suggest taking a closer look at the sample fcc gave you.
It will give you the basic structure on how to solve this challenge.

In the sample, we have an array called users

const users = [
  { name: 'John', age: 34 },
  { name: 'Amy', age: 20 },
  { name: 'camperCat', age: 10 }
];

The reduce method takes in a callback function and an optional initial value.

array.reduce(callback, optional initial value)

In the sample, we are using an initial value of zero and this callback function.

(sum, user) => sum + user.age, 0

The sum represents the accumulator or previous value from our callback function.
The user represents the current array value.

Basically what our code is doing is going through the users array and adding each of the ages and returning a single value.
In this case that return value would be 64.

You can run this code in your editor to see what values are outputted for each call of the callback function.

const users = [
  { name: 'John', age: 34 },
  { name: 'Amy', age: 20 },
  { name: 'camperCat', age: 10 }
];

const sumOfAges = users.reduce((sum, user) => {
  console.log(`The current value of the accumulator(${sum}) + current user age(${user.age}) = ${sum + user.age}\n The accumulator is now ${sum + user.age}\n`)
  return sum + user.age
}, 0);

Reduce is a tricky topic to understand at first.
It will probably take a few more articles, videos and practice before it makes total sense.

But for now, just heavily study the sample given to you and model your code after that for the challenge.

Hope that helps!

const factoryGuys = [
{ name: ‘Columbus’, age: 26 },
{ name: ‘Ade’, age: 26 },
{ name: ‘Ola’, age: 27 },
{ name: ‘Ismail’, age: 27 }
];
const sumGuysAges = factoryGuys.reduce((sum, factoryGuys) => sum + factoryGuys.age, 0);
console.log(sumGuysAges);
i did this on my own in other to understand how reduce works.yet i cant still solve the challenge

What’s your current code for the challenge?

As said, post your latest code so we can see what you have.

  1. You don’t have to but you may want to filter the watchList array first so you just have an array with only the Christopher Nolan objects. Then run reduce on that array.

  2. You know how to add the ratings together as shown by the age example.

  3. After adding all the ratings together the number needs to be averaged. You can use the filtered array length for that or when only using a single reduce some variable used to keep track of the number of numbers added together. The last requirement is (IMHO) very poorly worded but it basically just means you can’t hardcode the number you use to get the average.

1 Like

const averageRating = watchList.filter(film => film.Director === "Christopher Nolan")

The challenge is called ‘Use the reduce Method to Analyze Data’. You probably can’t have a valid solution that doesn’t use reduce.

That could be the start of a potential solution, as it filters to make a list of all films directed by Christopher Nolan.

const averageRating = watchList.reduce((data, { Director: director, imdbRating: rating })

I don’t see a valid callback function, let alone something with a return value.

const averageRating = watchList.reduce((data, { Director: director, imdbRating: rating }) => {

  

}

Still no return value. Are you guessing syntax? Can you explain in words what you think that code snippit above does?