Stumped on the Profile Lookup Challenge - Javascript

I am stuck on the Profile Lookup challenge.

When I input the following code:

for (var i = 0; i < contacts.length; i++) {
    if (contacts[i]["firstName"] == name && contacts[i].hasOwnProperty(prop)== true) {
            return contacts[i][prop];
    } else if (contacts[i]["firstName"] == name && contacts[i].hasOwnProperty(prop) == false) {
            return "No such property";
    }
}

All of the checks are complete except for: 'Bob", “Potato” should return “No such contact” & “Bob” , “number” should return “No such contact.” HOWEVER, when I add an else or another else if such as --> else if (contacts[i][“firstName”] !== name) with a return statement of “No such contact”, all of the previous checks become not complete and everything resorts to executing this statement and returning “No such contact.”

I am completely confused how this would be affecting the other checks if the other checks were originally executing the first or second if statement. And I am confused why the final else or else if of (contacts[i][“firstName”] !== name) isn’t working.

I’m trying to solve this on my own and didn’t want to just look up the solution, but I’m currently stumped.

Thanks in advance for all your help.

Remember that a function stops running as soon as it hits a return statement. If you hit a return inside a loop, then no more values are checked.

Hi ArielLeslie, thanks for helping me understand this. Since each condition is mutually exclusive, would the return statements be the problem?

For example, when I add the else or else if statement at the bottom, the checks that were initially being caught on the first condition are then going down and getting caught on the last else or else if condition (in this case --> contacts[i][“firstName”] !== name).

What would happen if the contact with the correct firstName is at index 2?

Oohhh ok now I am understanding. How do I restructure it so it can iterate through the entire list and then after return the correct condition?

Well, what are the conditions for returning “No such contact”?

my “No such contact” was going to be:

else if (contacts[i][“firstName”] !== name) {
return “No such contact”;

Just tell me logically. How do you know that “No such contact” is the correct answer?

I know when it is “No such contact” when the name does not correspond with any firstNames in the contact list.

Ok. When can you know that the name does not correspond with any firstNames in the contact list?

1 Like

When the loop has iterated through the list and has not been executed by the first two conditions?

Therefore, set a condition outside of the loop?

What condition will you be checking for?

A condition checking if the loop iterated through the entire list? Because that would mean that the two initial conditions did not execute.

Well, if the code is outside of the loop and placed after it, then that code would only be reached if the loop had iterated through the entire list.