Hi all,
I have met all off the requirements to pass the challenge, but there is one problem…
So, the first part where firstName == firstName in the array and property == property in the array only works by itself. Once I add “else if” or “else” to pass the second part, where the function returns “No such contact” and “No such property” the first “if” stops working, but the second the rest of the code works properly.
I am seriously stuck here.
Take a look:
//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].firstName && Object(contacts[i]).hasOwnProperty(prop) ) {
return contacts[i][prop];
}
else if (firstName != contacts[i].firstName) {
return "No such contact";
}
else if (Object.keys(contacts[i]).indexOf(prop) != prop) {
return "No such property";
}
}
}
// Only change code above this line
// Change these values to test your function
lookUpProfile("Bob", "number");