Dear staff, please show us the "right" solutions to FCC problems

freeCodeCamp features lots of challenges like “write an algorithm that does X”. I do write them, but then keep wondering whether I solved it “right”. Think performance and everything. For example, if I understand it correctly, this is basically a nested loop with the big O notation being n² which is a performance killer. I guess there’s a better way to do it. It’s a solution to this challenge

function whatIsInAName(collection, source) {
  const objArr = Object.entries(source);
  return collection.filter(colEl => {
    return objArr.every(arrEl => colEl[arrEl[0]] === arrEl[1]);

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

It would be a great help if once you pass the tests, you’re shown a “best practice” solution written by freeCodeCamp staff. Should I tag someone?

The Hints section contains many possible solutions. I think its a good idea to look at the solutions given in the Hints after you solve a challenge.

For the challenges in the section you linked, the main point is to get code that functions. The introductory curriculum isn’t really set up to handle questions of ‘optimality’ or best practices. For those questions, I’d create posts on the forum asking for feedback on your solutions.

1 Like

I looked at the solutions to that particular problem, and they all rely on nested loops

I can’t think of way to accomplish this task without some sort of nested loop.

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