How to finish...Wherefore art thou Challenge

How to finish...Wherefore art thou Challenge
0

#1

Tell us what’s happening:

I’ve been working on the logic of this challenge on paper, and I like what I have in terms of code so far. However I’m stuck with regards to each object in the collection must match each key/value pair in the source object. I do realize that the code is incomplete.

Please give guidance as to what I can do to complete this code. Thank you.

function whatIsInAName(collection, source) {
  
  var matchedObjects = [];
  
  collection.forEach(function(objectArgument) {
    var objectProps = Object.keys(objectArgument);
    
    for (var key in source) {
      if (objectProps.includes(key) && objectArgument[key] === source[key]) {
        
      }
    }
  });
  return matchedObjects;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

I think you need to try and fail a few times. I’m hesitant to give advice to someone who hasn’t tried a complete solution yet.

I will comment on the line:

if (objectProps.includes(key) && objectArgument[key] === source[key])

Because of order of operations, afaik, it evaluates objectArgument[key] === source[key] first and then does a logical and of the result with objectProps.includes(key). I assume what you want is:

if (objectProps.includes(key)  === source[key] && objectArgument[key] === source[key])