Wherefore art thou - get stuck

Tell us what’s happening:

Hello, third day i can’t figure it out, im passing 3/6 tests, but those tests with multiple properties, doesn’t work. Maybe someone can help me with hint or something. Thank you! :slight_smile:

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  let prop = Object.keys(source);
  let value = Object.values(source);
  for (let i=0; i<collection.length; i++) {
    for (let j=0; j<prop.length; j++) {
      if (collection[i].hasOwnProperty(prop[j]) && collection[i][prop[j]] == value) {
        console.log("true");
        arr.push(collection[i]);
      }
    }
  }
  console.log(arr);
  return arr;
  // Only change code above this line
}

whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

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

You’re close. The logic is a little off, though – what you’re saying inside your inner loop is “if this object has this property, go ahead and push it onto arr.” Which isn’t quite what you want. You only want to do that if “this object has all these properties.”

The logjc is slightly different:

let properties = Object.keys(source) /* yup, this works fine, as you're doing */
for /* Use some mechanism to loop over each member of collection */ {
  /* I'm using a "flag variable" here -- by default we'll include this member */
  let includeThisOne = true; 
  
  for /* Use some mechanism to loop over **EACH PROPERTY** of properties */ {
    if(/*does this member **NOT** have the property, or does the member property **NOT** match the source property ? */ )
    // In this case, set the includeThisOne flag to false;
  }
  // Having gone over all the properties, if **ANY** of them set `includeThisOne` to false
  //   We don't want it.
  if (includeThisOne)
    /* Here,do the pushy-pushy thing
}

1 Like

Omg, I forgot to look at ALL values in inner loop.

Thank u so much for your effort to help. :slight_smile:

1 Like

So did you pass? I’m really curious now!! rofl

Ye, I passed. :slight_smile: That thing when u “flag” to false if something is wrong, i was tried in very similar way before so it wasn’t a big deal, but somehow i forgot to check all values, that was my biggest mistake this time. :smiley:

1 Like