Why is my "Profile Lookup" challenge code not working?

I was quite pleased with my attempt but it fails the challenge.
When I check the code against the solution it seems very similar.
I’ve just used a variable to hold the contact object for each iteration.
If you can see why it’s wrong let me know.

The orginal is here.
The solution page is here.



//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    }
];


function lookUpProfile(firstName, prop) {
// Only change code below this line
  var contact = "";
  for (i = 0; i < contacts.length; i++) {
   contact = contacts[i];

  
   if (firstName === contact.firstName) {
     if (contact.hasOwnProperty(prop)) {
       return contact.prop;
       } else {
         return "No such property";
     }
   }

return "No such contact";
  
  
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

2 Likes

Two things:

  • You put the return "No such contact" inside the for loop. This way if the first item does not match the firstName it will directly return "No such contact" and not check the next items. (Since return exits the function.)
  • Also do you know what the difference is between contact.prop and contact[prop]? If not search for dot notation vs bracket notation. If you do know, you will also know what you did wrong.
3 Likes

I thought I did, but this has been a good opportunity to clarify and galvanise.

(for those finding this thread)

dot.notation

  • If you know the name of your property

bracket [notation]

  • If the property has a space in it e.g., “the entree”
  • If the property is a variable
5 Likes

I did get muddled with the formatting while trying out different things.

Is there a way in freecodecamp to automatically clean up the indentation (like in Sublime)?

1 Like

A post was split to a new topic: Guibuck90 - profile lookup