Profile Lookup - confused with an else statement behavior

Why isn’t this code working? It seems life the if/else statement should run all possible scenarios yet when I run this it only works with the arguments that would return “no such…” However, if I remove the last else statement and place the return “No such contact” in the function after the for loop it works fine. I really don’t understand this difference and would love some explanation.

My 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){
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }else{
        return "No such property"
      }
    }else{
      return "No such contact" //this else statement seems to be a problem somehow
    }
  } //when return "No such contact" is placed here without the else statement it works fine
// 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 (Macintosh; Intel Mac OS X 10_13_6) 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

RIght now, your algorithm works such that when you fail the first if, you to the else and return “no such contact”. Be careful when using return, because return exits out of the immediate function, not loop.

Isn’t that supposed to happen if it fails the first if? If name isn’t in the contacts then it should return “no such contact” and exit, right?

Not quite. You still haven’t checked the other 3 profiles right?

huh, so let me see if I understand this. As it’s written now it basically only executes the for loop once (essentially only checking the first contact), and never gets to check the other three. Whereas if I remove that last else, then it runs through the whole array.

function lookUpProfile(name, prop){
// Only change code below this line
  for (var i=0; i<contacts.length; i++){
    if (contacts[i].firstName === name){
      if(contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      }else{
        return "No such property"
      }
    }else{
      return "No such contact" //this else statement seems to be a problem somehow
    }
  } //when return "No such contact" is placed here without the else statement it works fine
// Only change code above this line
}

Ok so here is your function above.
I will now rewrite your function in plain english. See if you like what it does when I do that.
=================================
Pick up the first contact (contact[0]) then
check if contact[0]'s first name is equal to the given name variable (let’s say “Harry” is the given name)
Hmm, is Akira equal to Harry? Nope!
Ok then, since they’re not equal, return “No such contact”.

Done!
====================

So what do you think? Did your algorithm do what it is supposed to do?

Yes. You are only running it once. If you remove the else statement and its contents, it will run through the whole array.

Only after it checks through the whole array should you return no such contact right?