Profile Lookup: Not getting desired result

Profile Lookup: Not getting desired result
0

#1

Tell us what’s happening:
The block of code is supposed to check for the ‘fiirstName’ in contacts and either return the referenced property ‘prop’ or return an error message.

Your code so far

//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
 
  for(var 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";
      }
    }
  
  return "No such contact";
    

  }
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:

Appreciate some advice.


#2

I’ll give you hints so you can solve it on your own.

!) One of your return statements is in the wrong place.
2) “contacts[i].prop” looks for the property “prop” instead of the value of the variable which is not what you want.

Hope you can solve it now. Let me know if you need more help.


#3

Thanks for the tip. Solved it.