Another profile lookup help thread

Another profile lookup help thread
0

#1

I know there is like 40 of these threads floating around but I for some reason still cannot figure it out. Here is my code:

function lookUpProfile(firstName, prop){
// Only change code below this line
for(var key in contacts){
if(contacts[key].firstName===“firstName” && contacts[key].hasOwnProperty(prop)===prop){
return contacts[key];}
else if(contacts[key].firstName != “firstName”){
return “No such contact”;
}
else if(contacts[key].hasOwnProperty(prop)!=prop){
return “No such property”;
}

}

Thank you


#2

Are you sure you want to compare with the string "firstName"?

.hasOwnProperty already returns a true or false. It doesn’t make sense to compare true / false with whatever string is contained in the variable prop.

Think about when you should return "No such contact"; in your code. Do you want to return this while you are still looping through your list of contacts?


#3

Ok so I think I got your hints on the first two parts but i’m still lost as what to do with the last one. I understand that I shouldn’t return because then it stops the function but the instructions say return no such contact so how should I code it so its correct? Here is the new code: function lookUpProfile(firstName, prop){
// Only change code below this line
for(var key in contacts){
if(contacts[key].firstName===firstName && contacts[key].hasOwnProperty(prop)){
return contacts[key];}
else if(contacts[key].firstName != “firstName”){
return “No such contact”;
}
else if(contacts[key].hasOwnProperty(prop)!=prop){
return “No such property”;
}

}


#4

Hi, if you are still looking for a solution, this is what I did translated in English :smile:

  • declare two variable set to false both. These variables help me to know if the list contains the firstname and the prop
  • Loop through the contact list
  • Check if current contact firstname equals firstname param and current contact prop is not undefined
  • if the condition is true, returns the contact prop
  • If I am still looping, then check if the current contact firstname is equal to the firstname param
  • if true, then the list contains the contact

After the loop

  • Check if the list contains the contact (if no, returns no such contact)
  • check if the list contains the prop (if no, returns no such property)

#5

You want to return "No such contact"; after looking through all of your contacts. If that line is inside the loop there’s the chance that it will be returned even though you haven’t checked all of the contacts yet (which can’t be right, because you haven’t checked all contacts yet. How can you be sure that there really are no such contacts then?).