Basic Javascript: Profile Lookup AND statement condition

My approach was to use an AND vs a nested if but it doesn’t seem to be working. Any ideas why?

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 i=0; i<=contacts.length;i++) {
    if(contacts[i].firstName === name) {
        if (! contacts[i].firstName.hasOwnProperty(prop)){
            return "No such property";
        }
    }   
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
    }
elseif (contacts[i].firstName != name)
   return "No such contact";
}

Not sure if you accidentally clipped the code, but you appear to be missing a } for closing of the function. The biggest issue is the else if. Your code will always exit the for loop after the first iteration, because you have a return statement for all the if/else code blocks.

Think about when you should actually return “No such contact”.

Interesting I didn’t think of exiting the for loop and how that related to what the real problem is asking for. I’ve updated the code to


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

and still no dice!

I think the issue is that I’m using the not (!) operator instead of the solution. I haven’t seen anyone use it yet.

contacts[i].firstName is a string, it can’t have a property
hasOwnProperty can be used on objects only

Just woke up. As @ilenia points out, the following line is asking the wrong question. This if statement is checking if the firstName property of the contact at index i does not have a property name referenced by prop instead of checking if the contact at index i does not have a property name referenced by prop.

if (!contacts[i].firstName.hasOwnProperty(prop)) {