Stuck in Profile Lookup

Stuck in Profile Lookup
0

#1

Well, this is my code:

function lookUpProfile(firstName, prop){
for (var x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===true) {
return contacts[x][prop];
} else if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===false) {
return “No such property”;
} else if (contacts[x].firstName !== firstName && contacts[x].hasOwnProperty(prop)===true) {
return “No such contact”;
}
}
}

Console says the return message is right when either firstName or prop are wrong, which leads me to think that both “else if” statements are right. So the problem must be at the first “if” or its subsequently “return” statement, but I can’t see where I’m wrong. Any clue?


#2

Can you be more specific. What are you console logging? What return message are you getting?


#3

If you mean that when you both contact and property are false, you get no message, that is because you’ve not created a statement to handle that situation.


#4

Sorry, this is my first post here and english is not my native language, I hope I’m not being unpolite or something :slight_smile:

So this is what the problem asks me to do:

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

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.

If firstName does not correspond to any contacts then return “No such contact”

If prop does not correspond to any valid properties then return “No such property”

My code works well when any of both statements are false (“else if” statements), but it doesn’t when both are true (“if” statement).


#5

I’ll post here the hole code to make it easier to understand for anybody:

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 x = 0; x < contacts.length; x++){
if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===true) {
return contacts[x][prop];
} else if (contacts[x].firstName === firstName && contacts[x].hasOwnProperty(prop)===false) {
return “No such property”;
} else if (contacts[x].firstName !== firstName && contacts[x].hasOwnProperty(prop)===true) {
return “No such contact”;
}
}
// Only change code above this line
}

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


#6

I"m confused, because if you run the above code it works. The only time it doesn’t work is if both arguments are not in the array.


#7

Well, I’ll check it again tomorrow and if it keeps not working I’ll try to cover the condition where none of them are right. If it doesn’t work either, I guess I’ll have to report a bug.

Thank you very much anyway for your help!