Reduce challenge

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

the code is using the reduce method to find the average IMDB rating of the movies

I see nothing in that code related to taking any action based upon the Director or imdbRating. How you see that code you have provided doing what you said that it does?

I don’t understand how you went from the age example to what you have now? Please reduce some arrays for us.

const numbers = [10, 2, 20, 5, 5];

const total = ???

const people = [
  {
    name: 'John',
    age: 20,
  },
  {
    name: 'jack',
    age: 20,
  },
  {
    name: 'jll',
    age: 20,
  },
];

const totalAgesOfAllPeople = ???

const daysVisited = [
  {
    name: 'John',
    days: 20,
  },
  {
    name: 'jack',
    days: 20,
  },
  {
    name: 'jll',
    days: 20,
  },
  {
    name: 'jack',
    days: 20,
  },
  {
    name: 'jack',
    days: 20,
  },
];

const totalDaysByUserJack = ???

2 Likes
const numbers = [10, 2, 20, 5, 5];

const total = 0

const people = [
  {
    name: 'John',
    age: 20,
  },
  {
    name: 'jack',
    age: 20,
  },
  {
    name: 'jll',
    age: 20,
  },
];

const totalAgesOfAllPeople = numbers.reduce((accumulator, currentValue) => {
  return accumulator + currentValue
}, 0)
console.log(totalAgesOfAllPeople)

I’ve edited your post for readability. 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 (’).

This doesn’t use the reduce method at all, so it can’t be correct. Also, that isn’t actually the total of the numbers in the array.

This uses the numbers array, not the array of people. This reducer callback won’t work for the array of people. You could have used this above though.

after i removed this const total = 0 i am getting 42 on my console.log

But that isn’t the total ages of all people. How would you use reduce to compute the total age of all people in the array?

const numbers = [10, 2, 20, 5, 5];

const people = [
  {
    name: 'John',
    age: 20,
  },
  {
    name: 'jack',
    age: 20,
  },
  {
    name: 'jll',
    age: 20,
  },
];

const totalAgesOfAllPeople = people.reduce((totalAgesOfAllPeople, people) => totalAgesOfAllPeople + people.age, 0);
console.log(totalAgesOfAllPeople)

I’ve edited your post for readability. 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 (’).

Maybe you could try formatting your code yourself in future posts!

Hi @columbuschidozie1 !

Here is what I have observed so far in this conversation today :slight_smile:

The biggest issue I see is that there is still confusion on what the actual challenge is.
Remember that your task is to get the average IMDB rating of the movies directed by Christopher Nolan

@lasjorg was nice enough to break down those steps for you.
Right now you are providing small code snippets so it is difficult to know what your full solution is right now.

Take some time to slowly go through each of those steps provided for you and using console.log along the way to check the results you are getting.

Then you can come back to the forum and show us the full code of what you have tried and we can workshop it from there.

I understand these challenges are tough but these challenges will help you with your problem solving once you get it.

Also, these challenges are a precursor to what is to come in the future section.
So it is important that we strengthen your problem solving skills and basic understanding of these higher order functions.

Keep trying with this challenge and let us know what you tried :slight_smile:

As a sidenote, this is bad habit to develop and could cost you a job in the future.

If you are working in a company and your project manager is constantly telling you the same instructions and you keep ignoring it and making the same mistakes from project to project, they will get fed up and just let you go.

Just friendly piece of advice, if someone gives you instructions, please try hard to remember them for the future. :slight_smile:

Hope that helps!

1 Like
function getRating(watchList) {

  // Only change code below this line

  const list = watchList.filter(movie => movie.Director === "Christopher Nolan");

  

const averageRating = watchList.reduce((averageList, movies) =>{

  return averageList + Number(movies.imdbRating)

}, 0) / watchList.length

  // Only change code above this line

  return averageRating;

}

console.log(getRating(watchList));

Please format your code. Please. Have you read the instructions we have given you on how to do so?


That looks really close to the posted solutions, so you are getting close. Good job figuring out so much since the last code you posted.

This doesn’t really make sense. Why divide by the length of the original list?

Hi @columbuschidozie1 !

You are really close.

Here is the thing you need to focus on.

You went through the trouble of creating this variable here which is correct.

But then you don’t use it anywhere else in your code.

You need to use the list variable in your code in order to pass the tests.

Hope that helps!

function getRating(watchList) {

  // Only change code below this line

  const list = watchList.filter(movie => movie.Director === "Christopher Nolan");

  

const averageRating = list.reduce((sumOfRating, movies) =>{

  return sumOfRating + Number(movies.imdbRating)

}, 0) / list.length

console.log(list)

I formatted your code for you. Please do it for yourself. It makes your code easier to read for the people who you are expecting to help you. It is good manners to be polite and help the people who are helping you.

1 Like

A few things.

No1:

It looks like you have a syntax error.
Take a look at the console and read the error message.
It will tell you where the error is.

No.2:
It doesn’t look like your function is returning anything.
In earlier versions of your code, the getRating function was returning a value.
But in this latest post, it looks like you deleted the return value.

Once you fix those two issues, then the test will pass.


function getRating(watchList) {

  // Only change code below this line

  const list = watchList.filter(movie => movie.Director === "Christopher Nolan");

  

const averageRating = list.reduce((sumOfRating, movies) =>{

  return sumOfRating + Number(movies.imdbRating)

}, 0) / list.length

console.log(list)

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