i’m using contacts[contacts.length] to read all the object, so it doesn’t get out of the bounds: also i have to fix it to this i < contacts.length
can we return No such property in else block? but it will fail if first object doesn’t match name
i’m using contacts[contacts.length] to read all the object, so it doesn’t get out of the bounds: also i have to fix it to this i < contacts.length
can we return No such property in else block? but it will fail if first object doesn’t match name
you can’t add an else to the if you have, you go back to the issue of before
you are in the situation
you need to return or the property value or the No such property string, right?
so, how would you continue the sentence, “If the name match then…” to explain what you need to do now?
if the name match then return it’s property
always? what if the property is not present?
try to write a sentence that describe what to do
if the name match then return it’s property
if the property is not present, return no such property
if the name doesn’t match, return no such contact
once you return you can’t do anything else, shouldn’t you check about the property before returning?
returning on the contact book example, you find that the name match, so you are on the right page, so now you need to search for the property, so what steps do you take?
let’s way to do this for now, one thing at a time
sure.
first we should find property:
if the name match
make another check for property inside first if:
if property exists: return property
else: return no such property
that seems a good plan, can you implement that in your code?
function lookUpProfile(name, prop){
for(let 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";
}
}
}
}
Make sure you use the exact string requested, check that the capitalization matches the requested string
Now we are looking for Bob
in the first page we find “Akira”, the name does not match. So what to do? Can you say now “No such contact”? or do you need to look at the other pages first?
we need to look all the pages, if it is no-where present in objects, then we should return No such contact
and thinking of the loop, where can you return No such contact?
in the first else block?
but it will close the function after first unmatched name
so the else is not correct. you have the loop inside the function, at what point of the function has the loop looked at all items in the array?
when i < contacts.length ?
what do you mean? i < contacts.length is true when i is 0, or 1, or 2 or 3, aren’t those the indeces of the 4 contacts you have?
when i == 3 the loop has looked at all items in the array
there is an item at index 3, tho, so how can you be sure that that item was already looked at?
if we go back at the contact look example, are you sure you have looked at all the contacts once you are on the last page? or after you finish looking at the last contact, then turn the page and you have closed the book?
after looking at last contact, then turn the page, if there is no other pages i have closed the book