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
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.
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.
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.