Wherefore art thou javascript challenge

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:

1 Like

A ‘return’ value is always attached to, and stops the execution of (with that value) a function.

That function will return true at the first iteration of the loop.

Same happens with the standard return false, but in that case you want to return false the first element that does not follow the conditional :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.