Help on "For" loop profile lookup

Can some one please help explain why I am getting one Bob to pass, but the second Bob is not passing on this challenge. I am confused why part passes but the other part does not.

   **Your code so far**

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

lookUpProfile("Akira", "likes");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

What is this checking, and why isn’t it what you want to be checking :slight_smile:

It checks for a second item in the string, but I have failed the first if statement because Bob does not exist. So the first if statement fails, which fails the for statement.

Even if I just use an if if without the else, I still cannot get it to fail Bob.

Maybe I am missing something how functions are resolved?

I don’t understand what you mean?

You know you are not in this case

(contacts[i].firstName === name) && contacts[i].hasOwnProperty(prop)

so

contacts[i].firstName !== name

OR

!contacts[i].hasOwnProperty(prop)

What happens if both halves are false?

I meant it checks for any other item in the array if my understanding is correct.

I think I was missing the !==name, but…

Once I add that it does not allow the original if statement to work. I know what the answer is with a double if statement without the && but, I was hoping I could get it to work this way as it logically seems like it should.

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

Let’s step back for a moment.

There are three possible returns your code should have.

IF (???) THEN return the property
IF (???) THEN return “no such property”
IF (???) THEN return “no such contact”

You have two of these correct

IF (the name matches and the contact has the property) THEN return the property
IF (???) THEN return “no such property”
IF (no name matches) THEN return “no such contact”

So, what should trigger the second case?

I understand the 3 if statements, and I can always get two of 3 to work. The 3rd statement alludes me with any combination of if statements I try with the && statement.

Also now I am confused, because the first statement is coming out false when I add:

if (contacts[i].firstName !== name) {
return “No such contact”;

The 2nd and 3rd statements are becoming true in the checker.

I said that your code to check the first and third cases was already correct in your original code.

You don’t have the correct condition in your else if to check for the second case. I’m trying to get you to articulate when you can say ‘no such property’.

Total understand and appreciate the help.

I finally had the break through this morning!

Blockquote if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop) !==true) {
return “No such property”;
}
}
return “No such contact”
// Only change code above this line
}

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.