A problem about Profile Lookup

A problem about Profile Lookup
0

#1

I cannot pass the test of “Profile Lookup”.

/* my code is below*/

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

Link to the challenge:

please help me to fix the bug.Thks!


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Your solution does not pass the following test:

lookUpProfile("Harry","likes") // should return ["Hogwarts", "Magic", "Hagrid"] and instead returns undefined

because of the following line:

return contacts[i].prop;

Remember that using dot notation with objects assumes there is an actually property of the same name after the dot. In this case prop is a variable, so you need to use bracket notation. With the way yours is written above, JavaScript is looking for a property of the object called “prop” instead of “likes” which is the value of prop.


#3

Thanks for your advice.I passed the test according to your suggestion.