Continuing the discussion from What is the explanation for the For Loop in the profile Lookup exercise? :
The challenge says
If name does not correspond to any contacts then return "No such contact" .
Why isn’t this incorrect? It is from a solution I saw on the forum
if (contacts[i].hasOwnProperty(firstName) === false){
value = "No such contact";
}
if I’m not mistaking what the above code does is: It checks if contacts has the property firstName NOT if the name corresponds to any of the contacts
Please I need clarification.
miku86
July 23, 2020, 6:57am
2
Hey there,
what does the [i] stand for?
My full code
function lookUpProfile(name, prop){
var value = ' ';
for(var i = 0; i < contacts.length; i++){
if(name !== contacts[i]['firstName']){
value = 'No such contacts';
}
if(contacts[i].hasOwnProperty(prop) === false){
value = 'No such property';
}
if((name === contacts[i]['firstName']) && (contacts[i].hasOwnProperty(prop))){
return contacts[i][prop];
}
}
return value;
}
You should link to the full code that you took it from for better understanding.
But anyways, I guess firstName is the contact name that’s passed to the function. In that case, the above code will fail, because none of the testcases have that “name” as a property name.