Profile Lookup - reeee-mix

Sorry to bring this one up again. I did review other threads that already went through the issues >>> link. But when I tried these solutions, I was still having the same issues (breaking one half of the test cases or the other half).

My code is pasted below. You can see the very last line is commented out. When I comment it out, the positive testcases work and when I put it in, only THAT testcase works. I think I am overwriting previous testcases (and hence the commenting out fixes them) but I can’t tell how to change the code to fix. Esp since My code seems to follow what is generally being said in this thread >>> link:

function lookUpProfile(firstName, prop){
// Only change code below this line
  var message = "";
  for (var i = 0; i < contacts.length; i++){
      console.log(i + firstName + prop);
      if (contacts[i].firstName===firstName){
        console.log("First names are matching");
          if (contacts[i].hasOwnProperty(prop)){
            console.log("This property exists!");
            message = contacts[i][prop];
            return message;
          }else 
            console.log("This prop does not exist");     
            message = "No such property";
            return message;
      }else 
        message = "No such contact";
//         return message;
  }
// Only change code above this line
}

Nice work so far!

However, let’s take a look at an example and see how it would run through your code. Also, you’re missing { and } which mess up the meaning of your code. I’d fix those first, even though in this case it doesn’t matter too much. Once you’ve done that, let’s find out what exactly you did wrong!

Okay, so the example. (And let’s go through this as if you’ve uncommented that line).

lookUpProfile("Harry", "likes");
  1. i is set to 0, and we check if contacts[i].firstName===firstName. It isn’t. It’ll only be true later on in the for loop
  2. The else bit is triggered, which has a return statement. This cuts off the for loop and immediately returns an incorrect value (unless of course the correct answer happens to be “No contact” by coincidence :wink: )

When you do comment it out, though, there’s no way to ever return “No contact”. Let’s take a look at what you’re trying to determine, in order to fix your code.

To fix the issue, you need to get rid of the else bit. Returning a value because one out of all the contacts didn’t match won’t work.

Instead, go through the for loop. If there are no matches, then you know that you should return “No contacts” and so you can place the return statement after the for loop.

Good luck!

1 Like

:christmas_tree::christmas_tree:

Thank you joker314!! You were exactly right.