Wherefore art thou_I don't understand something

Hello, I don’t understand why the code isn’t:

function whatIsInAName(collection, source) {
  // What's in a name?
  
  // Only change code below this line
  let srcKeys = Object.keys(source);
  return collection.filter(function(obj){
    for (let i = 0; i < srcKeys.length; i++) {
      if (!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
        return false;
      }
       return true;
    }
    
  });

// Only change code above this line
return arr;
}

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?
  
  // Only change code below this line
  let srcKeys = Object.keys(source);
  return collection.filter(function(obj){
    for (let i = 0; i < srcKeys.length; i++) {
      if (!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
        return false;
      }
    }
    return true;
  });
  
  // Only change code above this line
  return arr;
}

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_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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

Hi,

The first code is too quick to return true. If the first property is a match true is returned without testing the others

{ apple: 1, bat: 2 } would match { apple: 1, cow: 2 } based solely on the first property and return true

The second code only returns false if there is no property or property values do not match and only returns true when all properties have been proven not to be false.

1 Like