Passing in Argument for an Array of Objects

Hey, I understand the logic in this challenge. However, there is a concept that I don’t quite get. We have a function with two parameters called Name and Prop. When we pass in an argument for firstName, how does it know to pass in for name? Also, when we pass in an argument for prop and called on, how does it know to look for lastName, number, and likes? It would make more sense to me if there were four parameters to have four arguments to pass in.

Here’s a link for your reference https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

I don’t understand what you are asking. The function arguments are a name and a property. You need to check if a contact with the provided first name has the property prop.

Sample function calls:

lookUpProfile("Kristian", "lastName");
lookUpProfile("Sherlock", "likes");
lookUpProfile("Harry", "likes");
lookUpProfile("Bob", "number");
lookUpProfile("Bob", "potato");
lookUpProfile("Akira", "address");

Right the function names are name and prop. But when you’re looking at the contact array of objects, its format is like this below. If I ask for Akira, it should look for Akira because of the argument Name, how does it know to look for lastName, number, and likes through the argument for Prop?

 {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

you need to make a function that finds the object with matching firstName and then get also the wanted property value.
there is no “how does it know”, because it doesn’t (and what’s “it”?), you need to create the logic for that

1 Like

Just for clarification, by passing any arguments through Prop, the function knows to look for the matching property name of firstName, lastName, and likes correct?

What do you mean by ‘knows to look’? You have to write the logic that looks up the property name stored in prop to check if the contact with the first name matching name has the property prop. That property name passed in might be lastName, it might be likes, it might be any string.

Well, once you have found the object with the matching FirstName, You can use Object.hasOwnProperty method to check if the the prop specified exists on the object.

I added spoiler tags since you gave away a significant piece of the solution logic.

2 Likes

Hi @NathanVu

I understand what you are trying to say, I was in the same position as you. You have this callback function.
You have 2 arguments and they are in order so whatever you have first and second arguments should be they should be also in your other function as well. Example

function ("Kristian", "lastName");
name = 'Kristina' 
lastName = 'Vos'

The first value will always be the name and the second value will always be lastName.
Now if you get that concept right, them when you pass these arguments as parameters in the function:

function lookUpProfile(name, prop) {

}

then you know name = Kristiana and prop = lastName
after this, you will do the logic by getting the values from the array and access the object

contacts[x].firstName === name // you know name will be Kristina 

In this case you are giving the lastName so you can do the logic of accessing the lastName from the Array of objects if there is such property or not.

contacts[x].hasOwnProperty(prop) // you know props its your lastName as property.

Hope this help let us know.

1 Like

Thank you for your response. I’m getting a better understanding of it. If what you’re saying is true, we have 2 arguments and they are in order. Name and firstName will go together, but how do you distinguish with prop when you have 3 other objects such as lastName, number and likes? Unless you wrote the function like this function lookUpProfile(name, lastname, number, likes) wouldn’t this make sense?

OMG. I reread this, I think I understand now, and hit the " AH Ha" moment.

1 Like

There is no lastName or number or likes argument.

There are exactly two arguments always provided in the exact same order. Never more. Never less. Never any different order.

lookUpProfile(name, prop)

The name argument always refers to the firstName

   {
        "firstName": "Akira", // 'name' refers to this part of the object
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },

The prop argument always refers to the key for some property that you are looking for

   {
        "firstName": "Akira",
        // 'prop' could contain "lastName", "number", "likes", "address", "potatoe", "someOtherKeyThatIsNotPresent" 
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },

It is your job to make the code find the contact that has a "firstName" that matches the given name.

If you find the contact with the matching name, then you need to determine if the contact has the property with the key prop.

If you find a macthing contact with a matching property, return the value of that property.

If name does not correspond to any contacts then return the string No such contact .

If prop does not correspond to any valid properties of a contact found to match name then return the string No such property .


You should be able to test your code with the sample outputs:

lookUpProfile("Kristian", "lastName");//  should return the string `Vos`
lookUpProfile("Sherlock", "likes"); // should return `["Intriguing Cases", "Violin"]`
lookUpProfile("Harry", "likes"); // should return an array
lookUpProfile("Bob", "number"); // should return the string `No such contact`
lookUpProfile("Bob", "potato"); // should return the string `No such contact`
lookUpProfile("Akira", "address"); // should return the string `No such property`
1 Like

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