Whats the mistake in this code?

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

**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(let i = 0; i < contacts.length; i++)
{
    if(contacts[i].firstName === name)
    {
        if(contacts[i].hasOwnProperty(prop))
        {
            return contacts[i][prop];
        }
        else
        {
            return "No such property";
        }
    }
    else
    {
        return "No such contact";
    }
}
// Only change code above this line
}

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

User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Hi @ij007 , welcome to the forum.

With the code you wrote if I run

lookUpProfile("Kristian", "lastName")

I get as result

"No such contact"

Which is not the desired outcome.
This is happening because your first if/else condition.

Since contact[0].firstName !== "Akira" you are returning that the contact is not present. But how can you tell on just the first iteration of the loop?

Hope this helps :sparkles:

2 Likes

Thanks a lot .
Have a good day/night :slight_smile:

You have a nested if statement the last else statement could be written by simply closing the whole if/else statement block and writing it in the function code block without an else included.

SOLUTION REDACTED

It is great that you solved the challenge, but instead of posting your working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting working solutions.

1 Like

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