I have passed all tests and solved the challenge. But I have a question about the return true
in the collection.filter()
. When I move return true
inside the for loop like this:
for (let i = 0; i < sourceKeys.length; i++) {
if (!obj.hasOwnProperty(sourceKeys[i]) || obj[sourceKeys[i]] !== source[sourceKeys[i]]) {
return false
}
return true
}
It doesn’t pass all tests. Why?
**My code so far**
function whatIsInAName(collection, source) {
let arr = [];
// Only change code below this line
let sourceKeys = Object.keys(source)
arr = collection.filter((obj) => {
for (let i = 0; i < sourceKeys.length; i++) {
if (!obj.hasOwnProperty(sourceKeys[i]) || obj[sourceKeys[i]] !== source[sourceKeys[i]]) {
return false
}
}
return true
})
// Only change code above this line
return arr;
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }))
;
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:96.0) Gecko/20100101 Firefox/96.0
Challenge: Wherefore art thou
Link to the challenge: