Profile Lookup?

Hi guys
I have a problem with this challenge

here is my code
where is the problem it seems to me logic as decribed in the challenge text!!? :roll_eyes::roll_eyes:

for( var i = 0; i < contacts.length; i++){
      if( contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) === true ){
    return  contacts[i][prop];
      } else if( contacts[i].hasOwnProperty(prop) === false ){
    return "No such property";
      } else{
    return "No such contact";
      }
}

The issue is that your code will not go beyond i = 0, because one of your three if statements is always executed, and when a return statement is executed the function stop working

Also, what about if the name doesn’t match? This doesn’t check that, but if the name doesn’t match but it doesn’t have the property this is executed.

And if the first two statements are not executed, there is always this. Maybe you need to return this if none of the names match (after each con arc has gone through contacts[i].firstName == name) - how can you do that?

so what I need to alter there? return to console.log() or what?

Your function needs to return something

You need to change the logic flow, change/move the if statements

1 Like

Thanks a lot for your Help the problem is fixed :grinning:

here is the problem, in the last code my function didn’t return anything?

Your function needs to return something

but the question is why the same statement was inside the for loop doesn’t perform the same output and when it’s out of the for loop work the right way?
here is the new code

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) === true ){
    return contacts[i][prop];
      } else if( contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) === false ){
    return "No such property";
      }
      
}

return "No such contact";
}
``
and the old one is above

Your function did return something, but you can’t change the return statements to console.log() because in that case without the return the function doesn’t return anything


Because if it’s inside the for loop the function would be able to check just contacts[0] and if that contact doesn’t match the name it returns No such contact and stop. If that return statement is outside the loop than first the function goes trough the array, checking each contacts[i].firstName against the name parameter, and if none of the objects in the array match then the No such contact is returned

1 Like

thanks a lot @ilenia for your answer :grinning: