I do not understand why the code breaks after i add the second part

When i add just the first -if- condition and check if it works it says the code is just fine and checks the top 3 tests. However, after i add the second part of the code, starting from -else if- for some reason it unchecks the top 3 tests and checks the bottom 3. I don’t understand what’s going on?

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){

for(var i = 0; i<contacts.length; i++){
if(contacts[i]['firstName'] === name && contacts[i].hasOwnProperty(prop)){
    return contacts[i][prop];
}
else if(contacts[i]['firstName'] !== name){
    return 'No such contact';
}
else {
    return 'No such property';
}

}
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Profile Lookup

Link to the challenge:

Welcome @comanche!

Taking a look at your code, the only tests that are passing are the ones that return a string of either “No such contact” or “No such property.” This is probably because the code is currently unable to access the correct information.
I would recommend taking a look at your return statements and think about when you want each of them to be used. It’s most likely the case that one of those statements is firing too early.

Hope this helps!

I got it. Tnx alot! :slight_smile:

1 Like