} else if(name !== contacts[i].firstName) {
return "No such contact";
Your problem is here. You are that if (on the first run through or whatever) that if that name does not equal that particular member’s first name, you want to return "No such contact"; Remember that return doesn’t just exit the for loop, but completely exits the function. So, unless the name asked about is the first one in contacts, then it will bale out of the function - it won’t even check any others. You need to find out of there is a way that instead of exiting the function, that you can just “continue” on to the next iteration of the for loop.
You are deciding (on the first run through) that if that name does not equal that particular member’s first name, you want to return "No such contact";
Hello again. I gave a shot. The below is my attempt.
I believe I understood what you pointed out and tried to solve that issue (keeping the function going instead of baling out on the first run). But now i have a new issue. The function doesn’t return “No such contact” when name doesn’t match any in contacts. I thought, after the for loop exhausts itself, “No such contacts” would get executed. Would you please take a look at this if/when you can?
function lookUpProfile(name, prop){
for (var i = 0; i < contacts.length; i++) {
if(name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if(!contacts[i].hasOwnProperty(prop)) {
return "No such property";
}
}
return "No such contract";
}
Hello I answered myself while capturing this below. If no name & no property, “No such property” gets executed before "No such contract. I am gonna fix this. don’t know how now, but will solve this.
Thank you. I could not solve it by myself. I had to look at the answer. But what you pointed out and your questions helped me understand better where I was wrong and why the answer was correct.