Lookup Profile JS

My head is exploding every JS exercise i’m doing haha, after some time thinking i don’t know what i am doing wrong anymore, can someone help?

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 arrayLength = contacts.length;
for (i=0; i <= arrayLength; i++){
  if(firstName == contacts[i].firstName){
    if(contacts[i].hasOwnProperty(prop)){
      return contacts[i].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");

You cannot use the dot notation with a variable

contacts[i].prop

points to a non-existent property prop, you want contacts[i].likes in this case. The way to access it is

contacts[i][prop]

The FCC platform isn’t a great place to debug. if you run into problems, I’d suggest you use an online IDE like repl.it and throw some console.log into your code to see how the variables are changing to see where the problem is

1 Like

Hi! I was struggling with this one too I got it to work by using logical operator “&&”.

This is my solution:

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

Man I was beating my head against the wall trying to understand where I was messing up, and it turns out I left the return “No such contact” portion inside of the if statement, resulting in anything that the loop didnt hit first turn giving me that message. Thanks!

2 Likes