Issue with the math on Use the reduce() method to analyze data

I’m getting a 414, so I’ll need the post the link to what I’m doing.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/use-the-reduce-method-to-analyze-data/

Here’s my code:

var rating = watchList.filter(myRating => parseInt(myRating["imdbRating"]));

var averageRating = rating.reduce(bef, aft) => {

var sum = (bef + aft) / rating.length;
});

return averageRating;

I think that my basic structure is correct, but there are some details that I’m skipping over/missing that are really screwing me over. I would like some guidance, please.

1 Like

Did you read the instructions closely? " rating of the movies directed by Christopher Nolan . [emphasis original]"

make it easy for yourself and sum everything with the reduce method and outside of it divide by the arr.lenght

watchList.filter(myRating => parseInt(myRating["imdbRating"]));

The filter method return an array of the items that return true with the callback function, you need to use map to do this

I’m not getting the reduce() right. I have an array of ratings, but I can’t seem to get the formula for finding the average right. What am I missing?

//Convert ratings into integers with parseInt()
var filmRating = watchList.map((myFilm => {
parseInt(myFilm.imdbRating)
}));

//Find the average of those integers
var averageRating = watchList.reduce((prev, curr) => {
return ((prev + curr) / watchList.length))
});

Have you tried console.logging the input values to the iterator function to see what is going on?

Console.log doesn’t register anything. I’m worried that I’m not instantiating them correctly. But, as much as I know about functional programming(so far), creating variables to do the dirty work so as not to mutate the globals as part of the whole thing. You’re allowed to create variables within a local scope. I thought this was what I was doing and that the program would follow suit.

Is there something I can change in the code in this regard?

Your reduce is iterating watchList, not filmRating. So that is the first thing. You want to use reduce to find the sum of your filmRating array. then divide that by watchList.length

@bananahair,

Have you solved your problems? If not, I am here, and, usually helpful :slight_smile:

1 Like

Thank you, @caraV
I’m hitting a small wall here. I know I have to assign another variable, but I think I could just do everything within the scope of averageRating; I’m sure the counter is good and I feel solid that it should work…but since it’s not working I’m highly skeptical of my inuition and I’d like somebody to tell me where I’m short-sighted and give some guidance about how to proceed.

var filmRating = watchList.map((myFilm => {
parseInt(myFilm.imdbRating)
}));
let counter = 0;
var averageRating = filmRating.reduce((prev, curr) => {
if(myFilm.director === “Christopher Nolan”){
counter++;
(prev + curr) / counter;
}
});

First, use reduce to accumulate the total value, increment that counter there if you like, then OUTSIDE the reduce() have the division by counter.

Also, note that when you are in your reduce(), you are no longer in your map() – so using myFilm is no longer an viable variable.

my personal experience with a couple of challenges on here was to delete and start over, or to simplify the initial criteria and console.log everything. A couple of weeks? ago, I had problems with object notation, list arrays, parameters, on this very problem, which is a good thing. I solved it then. I resolved the problem last night to see how my rationalization of it changed. I was able to do it all with the reduce function. One thing about the reduce, is it may be better for you to look at the first parameter in the anonymous function as the accumulator and the second the current iterated object in the array. After the function there is a second parameter in reduce used as an initializer. Passing a number variable initalized to 0 will start your accumulator off as a number object.

1 Like

I’m still, more or less, mucking through this code kind of half-aware of what I’m doing. But I do feel confident in are the following points.

  1. With map() I am using parseInt to apply conversion of all number-strings in imdbRating.
  2. I am setting “Christopher Nolan” as a definitive requirement for the return of data

Things I am not so sure of but have attempted
1.I have created two parameters, accumulator(prev) and the current value(curr).
2. These parameters, along with the specification of the director’s name are being passed through reduce() with specific designation that they will be added to each other and their sum will be divided the length of their array.
3. I know I should have a counter here and that’s what I assume the 0 is supposed to be but I’m, honestly, only 40% sure of why it’s there. I just have a feeling that it’s important and should be there.

This is what I’m working with and would appreciate your input.

Thank you for your patience (especially if you’re going to end up answering questions of mine that you’ve already answered before)
var filmRating = watchList.map((myFilm=>{
parseInt(myFilm.imdbRating)
}));
console.log(filmRating);
var averageRating = filmRating.reduce((prev, curr)=>{
if(filmRating.director === “Christopher Nolan”){
return prev+curr / filmRating.length
};
}, 0);

So here’s what you’ve got going on.

  1. filmRating is a .map() of the of the data from watchlist, but all filmRating contains is the integer value of the imdbRating, so `filmRating = [0, 4, 2, 4, 3, 2, 0] (for example). Note, there are no director properties. In fact, there are no objects here at all, simply integers.
  2. averageRating is a reduce() of that filmRating array, which again is an array of integers. So when you check if an integer has a property director, what do you think might happen?