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

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

#1

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

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

@BenGitter responded as I was typing up my own response. In addition to the prop issue, your indentations were hiding a problem. If I indent the code properly, you should be able to see why it is failing. Hint: It has to do with where you put your return “No such contact”.

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
}

#4

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

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)?


#6

Not in Free Code Camp, but if you use Codepen, you can click on on the drop down arrow in the upper right hand corner of the HTML section and select Tidy HTML. If you do not use code pen, then I have had luck using http://jsbeautifier.org/. You can copy/paste your code into it, and it will attempt to indent your code based on the settings you specify at the top.