Basic JavaScript: Profile Lookup - parentheses puzzle

Basic JavaScript: Profile Lookup - parentheses puzzle
0

#1

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!


#2

Your for loop only makes one iteration before you return a value. When a return statement is executed, the function immediately exits and does not come back to complete a loop.


#3

No, that is not what is happening. If you want to know what is being returned, put some console.log statements in to check which return statement is kicking out of the function for each test case.


#4

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.


#5

Because the working solution does not always return inside the for loop.

I just gave you a big hint at how to fix the code you originally posted.


#6

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.


#7

Having a return statement anywhere inside a function causes an immediate exit of the function. It can be located anywhere in the code (in a loop or not).

Think about why the FCC solution has a return outside the for loop to return “No such contact”. There is a good reason for this.

I corrected my previous reply to reflect what I meant when I posted it yesterday. I should have said “Because the working solution does not always return inside the for loop.” Sorry for any confusion I may have caused.


#8

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.