Profile Lookup: Why does my code not work?

Hi!
Please help me understand why the code I wrote for the “Profile Lookup” assignment does not work. Both “else” statements work correctly but the main “if” statement does not seem to trigger correctly. Have been thinking about this for a few days now and I think I need some help.

// 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 (var y=0; y<contacts.length; y++) {

if (name===contacts[y].firstName && contacts[y].hasOwnProperty(prop)==true) {

return contacts[y][prop];

} else if (name===contacts[y].firstName && contacts[y].hasOwnProperty(prop)==false) {

    return "No such property"

} else {

    return "No such contact"

}

}

// Only change code above this line

}

lookUpProfile(“Kristian”, “likes”);

1 Like

what’s going on is that your loop starts, the conditions are evaluated, if both the if and the else if condition do not execute, then the else does, and there there is a return statement. Once a return statement is executed the function stops and return a value, in this case that means that only the first item in the array is ever checked and values are returned based on that