Basic JavaScript: Profile Lookup Help!

This challenge is driving my crazy. Any help with it will be much appreciated.

This is what I got:

function lookUpProfile(name, prop){
var i=0;
while (i > contacts.length) {
    if (contacts[i]["firstName"] === name) {
        var j = 0;
        while  (j < contacts.length) {
        if (contacts[j].hasOwnProperty(prop)) {
            return contacts[j][prop];
          }
          j++;
          return "No such property"
        }
    } 
    i++;
}
return "No such contact"
}

Thank you!

One issue that I see is that there is no variable named “contacts”, so your while loop never executes, and goes straight to your return statement of “No such contact”.

Why do you think you need to loop through the contacts array once you have already found a contact where the firstName property matches the name variable? You just need to check if the current contact (the same one where the firstName property matched the name variable) has the prop property and then return it. Otherwise, just return “No such property”. It is a case where you just need an if/else without the while loop. If there where other contacts with the same prop being checked, then your function would end up return a prop value for another contact instead of the one where you already know the firstName matches.

1 Like

this while loop will never execute.

1 Like

Yes, you’re right. I removed that second loop and it works now.

Thank you!