Wherefore art thou with filter and for in

Tell us what’s happening:
I get three correct answer. But why?

Your code so far


function whatIsInAName(collection, source) {
  let filter = source;
  let result = collection.filter( x => {
      for (let keys in filter){
        if (x[keys] === undefined || x[keys] != filter[keys]){
          return false;
        }
        return true;
      }
  });
  return result;
}

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.13; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

You are returning true too hastly. Wait until your have checked for every instance of source before having your final say. I.e. move return true out of the for-loop block.

2 Likes

Thanks! That did the trick!