Intermediate Algorithm Scripting - Wherefore art thou

I see that hasownvals is not a method, however I don’t see how using hasownproperty is useful. Also why would i refer to Object.keys when the key isn’t important here, is it?

Your code so far

function whatIsInAName(collection, source) {
  // "What's in a name? that which we call a rose
  // By any other name would smell as sweet.”
  // -- by William Shakespeare, Romeo and Juliet
  const sourceVals = Object.values(source);

  // filter the collection
  return collection.filter(obj => {
    for (let i = 0; i < sourceVals.length; i++) {
      if (!obj.hasOwnValues(sourceVals[i]) ||
          obj[sourceVals[i]] !== source[sourceVals[i]]) {
        return false;
      }
    }
    return true;
  });
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

The key is important because that’s how you access the values associated with the keys.

Also, you need to find objects that have the same keys with the same values. It doesn’t count if the values are associated with different keys.

1 Like

Oh ok, I thought you could just use Object.values to access the values.

But if you only access the values, you won’t know if they correspond to the right properties

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