Finally after two days completed this challenge from Intermediate Algorithm Scripting

Just want to share my code
This was kind of hard so will spend time now trying to look and traverse through all the three solutions and see what could I have done differently.

   **Would love your feedback on my code**

function whatIsInAName(collection, source) {
 var arr = [];
 //console.log(source);
 // Only change code below this line
 arr=collection.filter(a=>check(a));
 function check(a)
 {
 var c=true;
 for(let name in source)
 {
 c&&=(a.hasOwnProperty(name) && a[name]===source[name]);
 } 
 return c;
 }
 console.log(arr);
// Only change code above this line
 return arr;
}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 });
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.177

Challenge: Wherefore art thou

Link to the challenge:

c&&=(a.hasOwnProperty(name) && a[name]===source[name]);

I think this is very awkward and makes the code seem more complicated than it needs to be. You can return false from the for loop as soon as either of those conditions fail. There is no need for variable c (not the best name for a variable by the way). If you make it through all iterations of the for loop then you know that the requirements have been met and you can return true. Getting rid of c will make this much cleaner and easier to understand.

There are other things you could do to make this more concise but the above is what stood out to me the most.

1 Like

I would try and avoid using a for (in) loop as much as possible as it is not nearly as performant as the other loops. A good alternative would be to make an array of the keys from the source array.

2 Likes

Thanks for the reply guys

The first thing I would do is start using a formatter. Professional developers work in teams, and you should follow formatting conventions to help others read and understand your code:

function whatIsInAName(collection, source) {
  var arr = [];
  //console.log(source);
  // Only change code below this line
  arr = collection.filter(a => check(a));

  function check(a) {
    var c = true;
    for (let name in source) {
      c &&= (a.hasOwnProperty(name) && a[name] === source[name]);
    }
    return c;
  }
  console.log(arr);
  // Only change code above this line
  return arr;
}

Now, I’ll add some comments as if I was reviewing code for a merge into my codebase:

function whatIsInAName(collection, source) {
  var arr = []; /* arr IS NOT A DESCRIPTIVE VARIABLE NAME */
  //console.log(source); /* DEAD CODE SHOULD BE REMOVED */
  // Only change code below this line
  arr = collection.filter(a => check(a));

  /* MORE READABLE TO DEFINE FUNCTION *BEFORE* YOU USE IT */
  function check(a) { /* FUNCTION NAME IS NOT DESCRIPTIVE */
    var c = true; /* LET OR CONST ONLY, NO VAR */
    for (let name in source) {
      /* USE return false OVER MAKING A VARIABLE - CLEARER */
      c &&= (a.hasOwnProperty(name) && a[name] === source[name]);
    }
    return c; /* RETURN TRUE */
  }
  console.log(arr); /* DEBUGGING STATEMENT SHOULD BE REMOVED */
  // Only change code above this line
  return arr;
}

Also, you are deep enough in the curriculum that you can try modifying code around the // Only change code comments to get cleaner results. I would do so in this case.


I would try to refactor on your own and see what you get:

SPOILERS! DON'T LOOK UNTIL YOU'VE REFACTORED
Summary
function whatIsInAName(collection, source) {
  function hasAllSource(entry) {
    for (const name in source) {
      if (!entry.hasOwnProperty(name) || entry[name] !== source[name])
        return false;
    }
    return true;
  }

  return collection.filter(entry => hasAllSource(entry));
}

There are more possible improvements, but this gives you the sense of how I go about reviewing code.

2 Likes

Hi @ankurchaulagain !

I have added spoiler tags around your code for those who have not worked on this challenge yet.


I also agree about formatting your code.
You can format it in the FCC editor by right clicking and choosing the format option.

Screen Shot 2021-06-09 at 8.40.14 AM

1 Like

Thank you guys. I appreciate it a lot :heart: :heart: :heart:

1 Like

Really enjoying the scripting challenges so far