Need help with Profile Lookup(JavaScript)

Need help with Profile Lookup(JavaScript)
0.0 0

#1

Ok tearing my hair out whats left of it:-)
The function should check if firstName is an actual contact’s firstName and the given property (prop) is a property of that contact.

If both are true, then return the “value” of that property.

Code below…

//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( firstName === contacts[i].hasOwnProperty(firstName) && prop === contacts[i].hasOwnProperty(prop)){
return contacts[prop];
}
}
// Only change code above this line
}

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


#2

There are a couple things you could think about here. The first thing I’d suggest is to read up on the hasOwnProperty method, and consider its return value. Note that you’re comparing the result of hasOwnProperty to the firstName and prop parameters, which are both going to be strings - does that make sense in this case?

Structurally (conditional logic aside), your function is going to return the correct answer in some cases, but not in all of them. If you look at the tests for that challenge, it expects three different kinds of output, depending on what you ask the function for: a string if there is no matching name in the contacts, a different string if the name exists, but the property doesn’t, and finally the value of the requested property, if neither of those two issues arose.

Basically, you’ll want to structure your if-else-blocks in such a way that your logic leads you to one of three ‘return’ statements - one for each outcome. Personally, I’d take an “error-first” approach - have a check to ensure that each part that -can- go wrong, isn’t, otherwise return immediately. For instance:

// input should equal "foo"
if (input !== "foo") {
  return "Wrong!"
}
// If the function progressed this far, input equals "foo"
return "Correct!"

And so on. Hope you found this helpful.


#3

Thank you Sakeran for helping, I finally figured it out after a while, I was sooo close for the longest time, grrr…but anyway a few less hairs on my head but got it eventually