Please help me understand why my code isn't working

Hi,

I’m trying to complete the profile lookup challenge on the javascript course. There is a problem with my first if statement but I don’t understand why? I had a look at the solution which makes sense to me but it uses nested if statements. Please help me understand what I have done wrong!

// 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 (let x = 0; x < contacts.length; x++) {
  if ((contacts[x].firstName === name) && (contacts[x].hasOwnProperty(prop))) {
      return contacts[x][prop];

    } else if ((contacts[x].firstName === name) && (!contacts[x].hasOwnProperty(prop))) {
      return "No such property"; 

    } else if (contacts[x].firstName != name) {
return "No such contact";
    }
}
}

lookUpProfile("Akira", "likes");

Challenge: Profile Lookup

Link to the challenge:

Consider that contacts array has multiple entries. Taking a look at if conditions in the loop, try to figure out what is happening when name is not the same as firstName of the entry in contacts.

Helpful can be also adding failing test case at the end of code and wrapping it in console.log() to see what function returns for certain case.

1 Like

In that case wouldn’t that mean the first two if lines in the if statement won’t be run as the conditions are not fulfilled. But the last else if will return “No such contact”?

When I run the tests

if ((contacts[x].firstName === name) && (contacts[x].hasOwnProperty(prop))) {
return contacts[x][prop];

is not working, is that related to firstName != name??

That’s right, your “No such contact” is returned whenever the first element is not the one you’ve searched for.

Your for-loop stops instantly when you return something. That’s what you could find, if you use some debug code like: console.log("test")
With that you could see, that your function terminates always at the first element.

So maybe you want to search your whole contacts before returning that you couldn’t find e.g. a specific person.

You could return something after the loop as well, if there was no fulfilled condition so far.

1 Like

Thanks I think I understand in concept what is happening but I am unsure what to do to resolve it? could you show me how you would change the code so that it will continue to loop though all the objects?

Well, the ‘no such contact’ response needs to occur after you have checked all contacts. At what point in your code has every single contact been checked?

Moving this return statement will make sure that you check every contact before you return that message.

1 Like

I kinda did.

Your first 2 conditions are okay.
Why would you prove your 3rd one on your first element of contacts?

If the contact you search for is at the 2nd position it never goes to that point, because of your third condition (which is fulfilled with the first element).

You should search for return contacts[x][prop] and return "No such property"; within your loop as you do, but return "No such contact"; should be out of it, since you only can say, that there is no contact, if you run through the for-loop completely.

Like:
You search for every contact in the for-loop and as soon as you found one, you return something.
If your program runs through your for-loop completely without returning something, you know: There is “No such contact”

2 Likes

Ah I understand now! Sorry now that I get it I realise you explained it perfectly before…
Thanks for your help it has been driving me mad all day!

2 Likes