Improved solution to „Use reduce method to analyze data“

I think my solution to the problem is more elegant than the one at https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-reduce-method-to-analyze-data/301313 and I‘d like to contribute it for use in the „Get a hint“ site, so that campers have an easier time understanding the solution.

function getRating(watchList) {
  // Only change code below this line
  let nolanFilms = watchList.filter((e) => e["Director"] === "Christopher Nolan")

  // Only change code above this line
  return nolanFilms.reduce((acc, e) => acc + parseFloat(e.imdbRating), 0)/nolanFilms.length;
}

What do you think about it? Is this possible?

I don’t think those are being maintained. Personally, I wish we’d get rid of them.

Sure, your solution works fine. It’s basically the same as the first solution without the added map. The only advantage to that that I can see is that it breaks apart that step a little and means that you don’t need an initial value for your reduce. I’m not sure that that is worth it. Yeah, if I were solving this at work, my solution would be similar to yours.

e["Director"]

Why the bracket notation?

Also, just in terms of “coloring inside the lines”, you weren’t supposed to change anything below // Only change code above this line - it may seem weird, but sometimes you have restrictions like that. For example, I usually try to not change any lines I don’t have to to keep my commits small and to avoid merge conflicts.

But again, I don’t think these are being maintained.

Why aren‘t they maintained? Why can’t we update it anyways? I think it‘s one of the most valuable parts of FCC.
Bracket notation is not necessary but I opt to use it most of the time to prevent errors and keep it similar to the way I access arrays.

I don’t know. I’m just repeating what I’ve heard. Like I said, I think we should remove them - they are more trouble than they are worth and they lead people to cheat.

Bracket notation is not necessary but I opt to use it most of the time to prevent errors and keep it similar to the way I access arrays.

That is a bad practice. You should break yourself of that habit. It will make your code look amateurish. You should only use bracket notation if the property name is stored in a variable or if it is not a valid JS identifier. I don’t know what “errors” it prevents, but I’ve never seen them.

Yes, I take that point. However, I found the provided solutions an invaluable learning tool. I would solve a particular challenge and then compare with all solutions. I gained useful insights into different (and often more elegant/efficient) approaches and picked up countless tips on how to think smarter about problem-solving.

Yeah, there is value in that - I do that on a lot of algorithm sites.

But a lot of these are not good - some are bad solutions, some just “showing off” with bad practices that look cool, some are based on old versions of the challenge and don’t work anymore. I think the mods got bogged down with correcting and updating them, campers were flooding them with their own versions, which were usually either not good or were just slight variations on existing solutions that offered nothing original. I think it just got to be too much work. And it was taking away from expanding the curriculum and helping campers.

There are sites that specialize in algorithms. You come up with a working solution, then you can see other people’s solutions, where there are comments and they can be ranked by efficiency, and people can vote, etc. Those sites are devoted to algorithms and getting the best solution. That is not FCC. FCC is about learning to code. It is about finding a solution as a tool to learn the language and coding.

If you want to look at the solutions, go for it. But take them with a grain of salt.

Alternatively you could show a link to an external site for ranking and discussing the best solution once a solution was reached. That way FCC would stay true to it‘s purpose while still allowing people to learn by understanding the best solution and preventing people from cheating. Which site were you referring to @kevinSmith?

As to changing it, this is a largely volunteer, nonprofit organization with limited resources and a lot to do. I don’t know if that is a priority. But I’m not part of those decisions.

As to which site I use - there are several. The one I’ve liked the most is leetcode.

Yeah, I mean, I like the idea of “hints”, but I think solutions are overkill. But you guys have to steer and prioritize as you see fit.

function getRating(watchList) {
// Only change code below this line
let count = 0 ;
let averageRating = watchList
.filter(movie => movie.Director===“Christopher Nolan”)
.reduce((avg, movie) => {
count++;
return (avg + +movie.imdbRating);
}, 0)/count;

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

This is my solution, but dividing at every iteration may not be the best idea.

function getRating(watchList) {
  // Only change code below this line
  let averageRating;

  averageRating = watchList
    .filter(({Director}) => Director === "Christopher Nolan")
    .reduce((acc, {imdbRating}, idx, arr) => (acc+ Number(imdbRating)) / (idx + 1 === (arr.length) ? idx + 1 : 1), 0);

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

console.log(getRating(watchList));