Intermediate Alg Scripting - Wherefore Art Thou

Tell us what’s happening:
I don’t understand why this code doesn’t work for this problem. Can someone help me understand? The goal is to return an array that contains all objects with the second argument passed to the function.

  **Your code so far**

function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
return collection.filter(name => name.hasOwnProperty(source));

// Only change code above this line
}

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/98.0.4758.82 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

The objects must have the same properties set to the same values.

sorry can you elaborate on that?

Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

source is an object. You want to find all of the objects in the array that have all of the same properties that source has, and those properties must be set to the same values.

1 Like

I really appreciate the help and I’m really sorry to keep asking but…

You say they need to be set to the same values. I’m confused as to what that means. Aren’t they all set as first and last names. I’m confused.

myObject["someProperty"] = hasThisValue

The ‘property’ is the name, or the thing in the brackets. The ‘value’ is whatever this property has been set to hold.

Ok so I see to an extent. I need to find the objects that contain the mentioned property, so in the example that would be “last”, with those properties set to the same values. Can you explain why my code above doesn’t work for that?

source is an object, not a string, so you are not checking what you want to check

there is no attempt to check for equality of value

1 Like

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