Profile Lookup exercise confusion

Tell us what’s happening:
Describe your issue in detail here.
Hi,

I’m a bit confused as of why it’s said to use “contact[prop]” to check whether given property ( prop ) is a property of that contact?

Another issue is why use “return ‘No such contact’” outside of the for loop…?

Thanks in advance
Your code so far


// 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(name, prop){
// Only change code below this line
for(const contact of contacts){
if(name === contact.firstName){
 return contact[prop] || "No such property"
}
} return "No such contact"
}

lookUpProfile("Akira", "likes")
**Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

For the code here, let’s presume we have an object called contact.

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

Because contact doesn’t have an address property, it’d be undefined.

const firstName = contact['firstName'];
console.log(firstName); // Akira

const num = contact['number'];
console.log(num); // 0543236543

const address = contact['address'];
console.log(address); // undefined

The || syntax used will return the first truthy value it finds. In this case, address isn’t truthy because it’s undefined because contact doesn’t have that property.

const value = null || undefined || address || "truthy";
console.log(value); // truthy

Does this help explain that part?

The loop is being used to find a contact. The loop will return once a contact is found which exits the function. The only reason the loop would ever finish without exiting the function is if it iterated every contact in the array, but none of them matched.

It can only get past that loop and continue if the contact doesn’t exist; hence you’d return "No such contact".

Hey @sethi ,

Thanks for your explanation !!! appreciate it !!! I got it now :)))

1 Like

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