How to replace the hard code by the property?

Tell us what’s happening:
My code seems to work for half the solutions. I just figured that I cannot have item.last hard coded like this on my filter callback. I ran out of inspiration of how can I replace this.

Any hints surfing around?

Your code so far


function whatIsInAName(collection, source) {
// What's in a name?

return collection.filter(item => {if(item.last === source ){ return item }})
}

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/79.0.3945.79 Safari/537.36.

Challenge: Wherefore art thou

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

Have you tried to write out an algorithm yet for this challenge? You should probably do that first before trying to write code.

source is an object, like item. So you need to filter() collection in such way, that every key-value pair of source will be present in item.

What you mean? I did all the previous challenges before this… Specify what you mean with algorithm?

An algorithm is the steps/logic needed to solve a problem. It is highly recommended to at least write out the steps in plain language or psuedo-code and validate the test cases would work with your algorithm and then convert your plain language to whatever language you want.

I need to loop trough collection.last and compare if the source object is = to that.
If so, I need to return the object that contains it.

That’s how i’m seeing it…

It is not quite that simple. Plus, last would only apply to just one of the tests. You need to think about how you would solve the tests without a computer, meaning you will need to perform certain steps (i.e. iterating, comparing various things) to arrive a the result expected for each test.

If you hardcode item.last as the comparator, the other tests will fail. The array being passed in (the one you see in the editor) is not the only array that your code should expect. Here’s another test using your same function:

whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) should return [{ "apple": 1, "bat": 2, "cookie": 2 }].

In that case, the source object has three properties. Your check needs to make sure your collection item has all three.