Profile Lookup function not working

Profile Lookup function not working
0

#1

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";
     }
    
  }
  
  
}


#2

ok, got it…

the last return should be outside the loop…


#3

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


#4

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");

#5

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


#6

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


#7

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