Basic JavaScript: Profile Lookup - parentheses puzzle

I think something about my {} is messed up but I cannot tell what! I have compared my code to other code and I even tried shifting around my else statements but to no avail. Help!

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

}

I am failing all but the following test-cases:

“Bob”, “number” should return “No such contact”
“Bob”, “potato” should return “No such contact”

So I suspect that my code is simply putting every test-case into the “No such contact” result. And those 2 test-cases passing is just a coincidence.

Thanks in advance!

OK, I tried adding a bunch of console.log statements. To little avail :frowning: I did however, uncover more questions that I have. Thinking out loud:

The code lookUpProfile("Sherlock", "likes"); should go through each item in contacts array and check to see:
1 - if it can find an object with firstName property value of Sherlock and;
2 - If so, check if that object has a property called likes.
3 - If yes: return value of likes property. Else return “No such property”.
4 - If both 1 and 2 fail: return “No such contact”.

I updated my code to set the variable answer at each of those steps but now I am having a problem of answer getting overridden with the next loop.

Also, how come the FCC solution has return statements without any problems?

Thanks.

I’m sorry to be so thick but I have pored over my (original ) code and compared it to the FCC solution and am just not getting it.

Having a return statement in the function causes it to exit but having a return statement in the for loop (which is inside the function) does not…? That is confusing enough but in addition, my return statement IS inside the for loop…isn’t it?

Also, the FCC solution has one return outside the for loop (return "No such contact";)…so add more confusion.

Sorry but I’m not getting it.

OK my code finally worked.

I think what frustrated me (partly) was that I thought I had already tried moving the final return statement outside the for statement but it hadn’t worked so I assumed that was not right. What I had forgotten (as I copied/pasted/copied/pasted) was that my original code had firstname instead of firstName. I fixed it at one point but then broke it again when I went back to old versions.

So ultimate lesson for me might be: make sure “code fixes” follow your different trial and errors of code. ARGH.