Profile Lookup; clarification needed for one thing

Profile Lookup; clarification needed for one thing
0.0 0

#1

So I had a lot of difficulty with this problem. I had to eventually look up the answer. Here it is:

//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    }
];


function lookUpProfile(firstName, prop){
// Only change code below this line
for (var x = 0; x < contacts.length; x++){
    if (contacts[x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("", "likes");

Basically, I went through the answer and realized my mistakes and how most of it was solved. However, I’m still not understanding how this code actually returns the firstName since the return doesn’t mention firstName (return contacts[x][prop]) I also don’t understand how the var “x” works in the return since wouldn’t returning"x" just give you the number from the array you found the matching firstName from. So I just need a little bit of clarification how it’s able to return the firstName that’s placed in the function.

EDIT: Nvm, I just realized the problem never asks you to return the name but just to search it.


#2

Let me try explaining.

You have an array of objects(an array inside which 4 objects are there) named contacts.
You need to check if a particular person is present in the array or not, and if yes, if the likes(you’re looking for) belongs to that person or not.

At the bottom you call the function, lookUpProfile(firstName, likes) which will run the function.
How the function works?
You’ll loop through the array, contacts with a for loop. That for loops will check how many objects are inside it, i.e. length. So you’ll iterate through each object!

Now, array starts with index 0 not 1.
So first object would be -> contacts[0]
2nd would be -> contacts[1] and so on.

Inside for loops, there’s an if check. When the first for loops runs, the if() will check if the firstName you’re looking for is present or not in the first object. If not, it will look at 2nd object. So, if not present in either of the 4 objects, it will reach the return statement will say “No such contact”. Obvious, right?

Now, say, your if() statement matches one of the firstName’s in one of the object.
Then, you see there’s an if statement inside the first if(). The inner If() will check if that object has the property you’re looking for or not. If yes, then it will return the value of property of that object.

Eg. say you searched for profileLookUp(“Akira”, “number”);
The first for loop will go through each object in the array and look for firstName “Akira” through the first if() statement. code -> contacts[0].firstName === “Akira” which is true
You can see, that name is present so next, it will go through the next if statement and look if “number” is present in that object through if() statement which has JS property- hasOwnProperty(“number”) which will be true so you’ll return that value of that “likes” property by returning contacts[0][“likes”].

Does this help or i messed it up? :joy:


#3

Do you remember earlier questions that involved accessing things in objects and arrays?

This is exactly like that! The return statement return contacts[x][prop] first finds the xth element in the array, which is an object, and then finds the prop of that object, and returns it!


#4

It helps clarify how I thought the problem worked. I just realized, the problem never stated to return the name but just to look it up to see if matches and return the property it’s asking for, so my mistake, thank you!


#5

Yah, I just realized the problem never asked to return the name but just to return the property of the name. My mistake lol.