Help with my Profile Lookup Challenge code

Someone tell what’s wrong with this code and a fix to it? Thanks!

function lookUpProfile(name, prop){
// Only change code below this line
    for(var i = 0; i < contacts.length; i++) {
        if(name === contacts[i]["firstName"] && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        } else if(name !== contacts[i]["firstName"]) {
            return "No such contact";
        } else if(prop !== contacts[i].hasOwnProperty(prop)) {
            return "No such property";
        }   
    }
}

Your function will stop executing immediately as soon as it encounters a return statement

Even if the “if” statement isn’t true???

One of those if statements will be true on the first loop iteration. And then your function will stop instead of continuing the loop.

That’s why the first array object is true but the other arguments are “No such contact”.

How can I fix this? I feel like I was on the right track but didn’t know/forget about the return statement stopping the function.

You want to say that you have no matching contact only after checking every contact. So where should that return statement be in your code?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.