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.
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.
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 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”;
}
}
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”
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’.
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
}