My code below returns array contain of every object that has a matching name instead of those with both matching name and value.
Please tell me what’s wrong with it.
Your code so far
function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
var arg = Object.keys(source);
for (var i = 0; i < collection.length; i++){
arr = collection.filter(value => value.hasOwnProperty(arg));
}
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.
that’s what’s your function does, keeps the objects that has property arg (remember that arg is an array so it is working right now only because arg has one single element inside. If it has more like in the other tests it doesn’t work.
also, note this is in a loop but you never use i so you are just doing multiple times the same operation
this is an algorithm with a bit of complexity, do it one step at a time. it will take time. it’s normal.