Profile Lookup - you need to get out of the for loop...(but why...)

Hi there,

I know there are lot of other threads for Profile lookup but I’d like to understand why my code doesn’t work.

Just so you know, I’ve been on FCC all day and I guess I am tired but for the life of me I don’t see what’s wrong.

It works for Bob and Akira but not for the three others… Can anyone help me?

function lookUpProfile(firstName, prop){

        for (var i = 0; i < contacts.length; i++){

        if(firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
        else if(firstName !== contacts[i].firstName){
        return "No such contact";
        else if(!contacts[i].hasOwnProperty(prop)){
        return "No such property";

Duh… looking up other threads (again) I deleted the second else if statement and put return No such Property after the loop.

Now it works.

written by svmi3195 : “
Just try write on the paper what happens with your variables at each step - it will be more clear. Now, if you put your return “No such contact” inside the loop, it just will not iterate through all possible names (if first name in contacts is not the same as the name value it is checking against it then will return “No contact” terminating the loop). You need to check against all names in your contacts, so you need the loop to run all iterations. In your decision you put it outside of the loop, so it works. In this case, if the name is in contacts list, another return statement will run and the program will not get to your “No such contact” return statement. Just understand that return makes it to go out of the function - after return runs, no other code in the function will run.”

I’ll try to read that p1xt recommends.

1 Like