I figured out that the hasOwnProperty function would do the trick, but i don’t know what’s happening.
> 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){
return contacts.hasOwnProperty(firstName);
}
lookUpProfile(“Akira”, “likes”);
Why does
"return contacts.hasOwnProperty(firstName);" return false?
A few things.
contacts
is an array, not an object so it won’t have a property.
Inside your function firstName
is an argument variable. This means that in the case of lookUpProfile("Akira", "likes")
, the variable firstName
is "Akira"
so contacts.hasOwnProperty(firstName)
is the same as contacts.hasOwnProperty("Akira")
.
Okay, that I knew.
So I would need to iterate through the array so I can check every object?
Maybe something like
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].hasOwnProperty) {
do something here
}
}
Am I on the right track?
That’s a step in the right direction. Keep in mind though that none of the objects in the array will have the property "Akira"
, so it will still be false.
Ah, so this was the problem
How can I access the left-part? The ones that’s not the property, was it called key?
“key” and “property” mean the same thing in this context. That’s the “left-hand side”. The “right-hand side” is referred to as the “value” or sometimes “contents”.
Okay, so, I think this should do the trick.
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[0].firstName) {
return true;
}
}
}
Looks like you’re on the right track. 
Okay, so, I’m nearly there:
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName) {
return contacts[i][prop];
}
}
}
However, I still have a problem with the function returning “No such contact/property”, because i can’t just do:
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName) {
return contacts[i][prop];
}
else {
return "No such contact"
}
}
}
Since this will break the function before i is able to add one to itself.
Any help would be appreciated
If you have already looped through all of contacts
without returning a value, then you know that there was no contact with a matching firstName
. The “No such property” bit is when you’d want to use hasOwnProperty()
.
So, this almost works:
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else {
return "No such property";
}
}
}
}
But the only requirement I have left it’s to return “No such contact” when the contact doesn’t exist, but I don’t know how to achieve that without stopping the [i] iteration.
Also, the code it’s getting hard to read, I hope I’ll learn how to write nicer code on the future
Great! Thank you, so thinking about that I realized that
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else {
return "No such property";
}
}
}
return "No such contact";
}
If the whole loop is done and it didn’t return anything, no firstName exists, so i can just return false.
Thanks! I thought i was over for a moment with this one.
2 Likes
This challenge throws a lot of people through a loop. You’ve got this. Happy coding!
1 Like