I tried the brackets notation and also the dot notation, but i dont understand how can i access an item inside of the array without pointing to its position (contacts[i][1]). could you explain it to me?
Thanks
I tried to remake it, but still no luck.
This is my Code.
function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0 ; i < contacts.length; i++){
if(contacts[i].firstName === name ){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}else{
return "no such property";
}
}return "no such contact";
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Here is another way that i tried and i got a few marks correct.
That was the way i tought it at first but didnt think it woud work.
Still, its not returning the no such prop and no such content correctly , but idk why.
function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0 ; i < contacts.length; i++){
if(contacts[i].firstName === name && (contacts[i].hasOwnProperty(prop))){
return contacts[i][prop];
}else if(!(contacts[i].hasOwnProperty(prop))){
return "no such property";
}
}return "no such contact";
}
// Only change code above this line
// Change these values to test your function
lookUpProfile("Akira", "adress");
function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0 ; i < contacts.length; i++){ //<- this loops through the array
if(contacts[i].firstName === name && (contacts[i].hasOwnProperty(prop))){// this means that if the first parameter submited on the function is equal to "firstName" AND it has a property listed in the array[i], it will return the requested parameters
return contacts[i][prop];
}else if(contacts[i].hasOwnProperty(prop) === false ){// if it doesnt have a listed property (2nd paramenter), it will return no such property.
return "No such property";
}
}
return "No such contact";} // if any of the past conditions doesnt pass, it will return no such contact.
// Only change code above this line
// Change these values to test your function
lookUpProfile("Akira", "adress");
although i understand where it can be improved, im not sure HOW to improve it. i tried break in it into diferent ifs and else ifs but it didnt work either. I also tried making the positive and negative forms of each requested statement and it didnt work.
At this point im not sure whats wrong. please walk me through it.
You are failing this test-case right?
Let’s break down your code according to this condition.
If the objects in contacts array have the firstName as name and they have property prop.
Here, name Bob is not in any it thr objects of contacts array and there is no such property as potato.
So, it moves on to next condition.
In this condition, your code checks if the contacts[i] does not have a property named prop i.e. potato in this case and returns “No such property”.
This is right therr is no such property named potato but there isn’t even a name called Bob.
You are not checking that.
You will have to add that condition in this else if statement.
As i mentioned here:
Try doing that and you should be good to go.
Hope this helps.
Instead of using for loop, I suggest to use the filter() method, which gets only the element that match with the name of the property ex: return contact.firstName === name;
When you get the match with the filter you can assigned to a new variable and validate if that array exist or not, and with that information you can validate no such property or no such contact
if (filterName[0]) {
return filterName[0][prop] ? filterName[0][prop] : 'no such property';
} else {
return 'no such contact';
};
@yamitrvg12 and @clevious, I agree with you guys.
But, the campers have to solve this one with for loop cause they have not been introduced to higher-order functions.