This code is not executing even if the conditions are correct

Yes Ariel, the contact has firstName “Akira” and execute else.
Is it due to var i is assigned to 0 ?

or, if i is looping through the contacts.( we are now inside for loop)
so the answer should be printed as :

"No  such contacts."
“Harry [‘Hogwarts’, ‘Magic’, ‘Hagrid’]”
"No  such contacts."
"No  such contacts."

which above one is correct?

Yes i have bookmarked the advice.
It is a good planning.
But first i have to clear some of my confusions here.

It is looping through the contacts BUT IT STOPS AS SOON AS THERE IS A RETURN STATEMENT.

This is what Jeremy and I have been trying to explain from the beginning.

1 Like

This is the solution for that section

function lookUpProfile(name, prop) { 
for (var x = 0; x < contacts.length; x++) {
    if (contacts[x].firstName === name) { 

if i call the name Harry( second name inside the array) the above code won’t execute to the next if statement. because the first if statement is false. Then it goes to else statement and print it. so it hit return and stop the execution. That is not happening here.
why is it so?

        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

Because your still hitting a return statement in the first iteration. I don’t know how many times I can say this. Nothing happens after a return line. If you have a return in your if AND in your else then only the first element will ever be checked.

Intended behavior of the code:

lookUpProfile("Akira", "likes");

Will do the following things:

  1. Check if the first contact has a matching first name. It does.
    a) Check if the contact has a property called likes. It does.
    b) Return the property likes and halt function execution.
lookupProfile("Harry", "lastName");

Will do the following things:

  1. Check if the first contact has a matching first name. It does not.
  2. Check if the second contact has a matching first name. It does.
    a) Check if the contact has a property called lastName. It does.
    b) Return the property lastName and halt function execution.
lookupProfile("Sherlock", "cases");

Will do the following things:

  1. Check if the first contact has a matching first name. It does not.
  2. Check if the second contact has a matching first name. It does not.
  3. Check if the third contact has a matching first name. It does.
    a) Check if the contact has a property called cases. It does not.
    b) Return the string "No such property" and halt function execution.
lookupProfile("Bibothy", "number");

Will do the following things:

  1. Check if the first contact has a matching first name. It does not.
  2. Check if the second contact has a matching first name. It does not.
  3. Check if the third contact has a matching first name. It does not.
  4. Check if the fourth contact has a matching first name. It does not.
  5. No remaining contacts to check.
  6. Return the string "No such contact" and halt function execution.
1 Like