Reputation: 395
I am doing a challenge from freeCodeCamp called "Profile Lookup". My problem seems to be that I cannot return the value of a property within an object. I am using dot notation, but have also tried using bracket notation.
The objective is to create a function that loops through an array of objects, contacts
, to find whether a given firstName
is within said array and whether that object contains a given prop
.
If both the firstName
and the prop
exist, we have to return the value of the property.
If the firstName
is found but prop
is not, then we return "No such property".
If firstName
is not found, we return "No such contact".
This is my code:
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.prop;
else
return "No such property";
}
else
return "No such contact";
}
// Only change code above this line
}
This is the array of objects that it is supposed to loop through:
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"]
}
];
EDIT: I must have dropped the [i] in return contacts[i].prop
when I changed to bracket notation and then back to dot notation. Even when I reapply it, I am getting the same problem. Here is the updated function:
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";
}
else
return "No such contact";
}
// Only change code above this line
}
Upvotes: 1
Views: 6998
Reputation: 5148
You have to return the value of the variable as key
return contact[i][prop];
By doing contacts[i].prop
you are returning the attribute named prop
: contact[i]['prop']
Complet Function
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";
}
else
return "No such contact";
}
// Only change code above this line
}
Also you're returning for every case something the first iteration.. Even if the name is later on the array. Let's try something
function lookUpProfile(firstName, prop) {
// Try to find the right contact
for(var i = 0; i<contacts.length; i++) {
// Name is found
if(contacts[i].firstName == firstName){
// Return prop if exist, or error message
if(contacts[i].hasOwnProperty(prop))
return contacts[i][prop];
else
return "No such property";
}
}
// No one with this name is found, return error
return "No such contact";
}
My way to do it (not tested)
function lookUpProfile(firstName, prop) {
let contact = null
// Try to find the right contact
contacts.forEach(c => {
if (!contact && c && c.firstName && c.firstName == firstname) {
contact = c
}
})
if (!contact)
return "No such contact";
return contact.hasOwnProperty(prop) ? contact[prop] : "No such property"
}
Upvotes: 5