Profile Lookup function not working

Can someone help me with profile lookup?

I’v rewritten this function several times, I cannot find the mistake:

function lookUpProfile(firstName, prop){
// Only change code below this line

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

ok, got it…

the last return should be outside the loop…

1 Like

Good job you are fast :slight_smile: Samba dancer ? :wink:

HI,
I did not know if that’s ok (too messy?), but I did in a different way.

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

// Change these values to test your function
lookUpProfile("Harry", "likes");
1 Like

I think that, if it works, its ok :relieved: :grin:

Hi nanquim,

Why is it important that the last return is outside the for loop?

Can you please elaborate why your original solution did not work? I have the same problem with my solution.

Ahmed

This one and the record collection were a struggle for me too!