Hello!
I have been stuck in this algortihm for some hours. I wrote 5 or 6 solutions that didn’t work for every item. Now, I finally solved it, but I feel it’s a stupid solution… at least very unorthodox. Can you please take a look at it and help me to think in a better way?
I think the basic algorithms were pretty easy and very enjoyable. However, I am taking a long time to solve these intermediate algorithms and when I do, it always seems that it’s not the best way at all.
Thank you for your attention! This is my code…
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var propSource = Object.keys(source);
var test = 0;
arr = collection.filter(function(obj) {
for (i=0; i<propSource.length; i++) {
if (obj.hasOwnProperty(propSource[i]) && obj[propSource[i]] === source[propSource[i]]) {
test += 1;
} else {
test += 0;
}
if (test === propSource.length) {
test = 0;
return true;
}
}
test = 0;
});
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });