Profile Lookup FCC Question RE nested if statement

Profile Lookup FCC Question RE nested if statement
0.0 0

#1

I’d like someone to explain why the code below does not work. I thought that by using && I would eliminate the need to nest two if statements.

 function lookUpProfile(firstName, prop){
      for (var i = 0; i < contacts.length; i++){
        if(contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)){
          return contacts[i][prop];
        } else{
          return "No Such property";
        }
        
      }
     return "No such contact"; 
    }

And why the code below this line works just fine

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

#2

The reason the first one will not work, is because if firstName is not equal to the the specific contact’s firstname the else statement will execute which is not what you want. The second version first checks if the first name matches. If it does, then you check if the property exists.