Profile Lookup not sure what is wrong

Tell us what’s happening:
My code works when the arguments are not found, but not when they are. Any ideas?

Your code so far


//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["JavaScript", "Gaming", "Foxes"]
    }
];


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

// Change these values to test your function
lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Oh, thanks. So the “No such…” should come first?

I am not sure, but it seems like the for loop would make it so that it would run through all four entries right away and know then whether the name is in the array, I mean, if I start with this:

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

Maybe there is something about a for loop I do not understand. I thought it keeps iterating, unless an if statement is true and then a value is returned. no?

Well, actually, I did understand that a return ended the loop. But if the if statement is false, does it keep iterating?

What I mean is if (contacts[i].firstName !== name) is false, does it not move to the next else if statement?

OK, so if I arrange it as follows, why is it not executing the third return statement when the firstName and prop are present in the array?:

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

write 'No such property" first
works for me

Thanks, Randell. Now I understand.