function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
arr = collection.filter(function(elem) {
for (var i in source); {
if (source[i] != elem[i]) {
return false;
}
}
return true;
});
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
[{ first: “Tybalt”, last: “Capulet” }]. <<< This is the output
First of all, the if statement checks,
if (source[i] != elem[i])
if capulet != elem[i])
the capulet IS NOT equal to any value in source array, and since the first object is already FALSE, the for loop should stop executing. How did it manage to return [{ first: “Tybalt”, last: “Capulet” }] … Arent we checking for FALSE? The first one is already FALSE so it should return [{ first: “Romeo”, last: “Montague” }]