Reputation: 365
I'm in the 217th challenge of freecodecamp which is profile lookup.
This is the problem definition
We have an array of objects representing different people in our contacts lists. 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"
I saw many using the equality operator within the "if" loop but I wanted to solve it using the "hasOwnProperty" function. I don't know where I'm going wrong.
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(contacts.hasOwnProperty(firstName) && contacts.hasOwnProperty(prop))
{
return contacts.prop;
}
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Upvotes: 0
Views: 1173
Reputation: 1
Here is the solution to this problem
function lookUpProfile(firstName, prop){
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == firstName && prop in contacts[i]) {
return contacts[i][prop];
} else if (!(firstName in contacts[i]) && i == (contacts.length - 1)) {
return "No such contact";
} else if ((typeof contacts[i][prop] == 'undefined')) {
return "No such property";
}
}
Upvotes: 0
Reputation: 1
The above code will work for one test case but not for all that are required to pass the session.
function lookUpProfile(firstName, prop){
for (i = 0; i < contacts.length; i++) {
if (contacts[i].hasOwnProperty(prop)) {
if (contacts[i].firstName == firstName) {
return contacts[i][prop];
}
}
else {
return "No such property";
}
}
return "No such contact";
}
Upvotes: 0
Reputation: 1476
You need to compare actual value of firstName
property, (contacts[i].firstName == firstName)
.
See more details in comments.
Here is working code
// Code goes here
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++){
// use i as array index, to access particular contact object
if((contacts[i].firstName == firstName) && contacts[i].hasOwnProperty(prop)){
//dot notation will not work here, use []
return contacts[i][prop];
}
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
console.log(lookUpProfile("Akira", "likes"));
Upvotes: 0
Reputation: 2326
contacts[i]
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(contacts[i].firstName == firstName && contacts[i].hasOwnProperty(prop))
{
//return contacts[i].prop;
alert(contacts[i][prop]);
}
//return "No such property";
alert("No such property");
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Upvotes: 0
Reputation: 1790
you just forgot to lookup for the object in your array, and not your entire array :
for(var i=0; i<contacts.length; i++) {
var contact = contacts[i];
if(contact.hasOwnProperty('firstName') && contact.hasOwnProperty(prop)) {
return contact.prop;
}
return "No such property";
}
Upvotes: 0
Reputation: 2757
The issue is firstName
in lookUpProfile
function is not a property. It is a value for firstName property.
when we call lookUpProfile("Akira", "likes")
, your code tries to check if the property "Akira" exist in the data (which does not) and it does not return the property. You need to replace contacts.hasOwnProperty(firstName)
in your if statement with contacts.firstName === firstName
In addition, you need to get contact in your loop:
var contact = contacts[i];
Upvotes: 0