Profile Lookup: Why is my method wrong

Tell us what’s happening:
My code fails for the first three question and i cant figure out why. I looked at the hints answers and i get why that works and i feel its essentially the same thing as mine, just worded differently.
It failed for these:
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array

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(name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
    return contacts[i].prop;
  }
  else if(name == contacts[i].firstName && !contacts[i].hasOwnProperty(prop)){
    return "No such property";
  }else if(name!=contacts[i]["firstName"]) return "No such contact";
}
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");
lookUpProfile("Sherlock", "likes");
lookUpProfile("Harry", "likes");
lookUpProfile("Bob", "number");
lookUpProfile("Bob", "potato");
lookUpProfile("Akira", "address");

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

Your code only iterates through the for loop one time before returning a value (contacts[0].prop, “No such property”, or “No such contact”. Remember, when a return statement is executed, the function is immediately exited.

Hint: Think about when you should return “No such contact”.

Hint: this return “No such contact” should be called when you search all contacts, and you sure there is no any contact. this is possible “Kristian” is not the first contact, but the last one.

@NULL_dev and @RandellDawson i dont get what you are saying. Lets look at lookUpProfile(“Kristian”, “lastName”)
It goes through the first contact and sees that it does not match and then it goes through the second and then the third until it finds Kristian. Isnt that what it does. Its still doing the first if statement is it not?

You have one if, and two else-ifs. when first if fails, so either 2nd or 3rd else-if would return true.

When it asks for “Kristian”, the first if fails, second fails too, but 3rd never fails! actually your last if-else works just like else.

More hint:
You should return “No such contact” when you check all elements, and sure there is no any contact name of given name. This could be happen after your loop.

It evaluates the first if statement conditions, which in the test case of lookUpProfile(“Kristian”, “lastName”) is false, so the first else if condition is evaluated, which also evaluates to false, so the second else if condition is evaluated, which is true, so the function returns “No such contact” and then stops processing anything else.