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?

All the solutions should work unless the challenge was dramatically changed in the past year or so and I have not seen any of them changed. I wrote a script that ported all of them over from the old Guides and the script checked to make sure each solution passed the tests of each challenge.

I am only talking about the solutions that are hidden in the summary/detail elements. The rest of the solutions I personally wish we would delete all of those. There was talk at one point in doing that, but with the new JS curriculum coming, there will no longer be solutions, so these solutions will probably just go away at some point once the new JS curriculum has been introduced.

1 Like

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.

The reason the solutions exist here on the forum is because we knew all the solutions were available all over the internet (I have my solutions to all the challenges on my GitHub account). We also knew that the forum guide posts for challenges had extremely high search rankings on Google. We would rather keep people coming to the forum on freeCodeCamp.org instead of getting lost on some other site.

The new JS curriculum will not have solutions posted on a Guide page because it will be project-based. Each step of a project will introduce or reinforce a previously learned concept/syntax. There will be multiple projects that will cover the same topics/concepts but also finally introduced DOM manipulation before getting into libraries like React.

Each proceeding step will start with the code needed to solve the previous step (just like the new RWD curriculum does today), so the steps are the solutions.