Profile Lookup - IF not true if ELSE present

Profile Lookup - IF not true if ELSE present
0.0 0

#1

I’m afraid I don’t understand something related to this challenge.

This IF statement renders true, and returns half of the challenge as correct.

for (i = 0; i < contacts.length; i++){
    if (contacts[i].firstName === firstName &&  contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop];
    }
  }

BUT, when I add an ELSE statement, the first half is not true anymore

  for (i = 0; i < contacts.length; i++){
    if (contacts[i].firstName === firstName &&  contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop];
    }else {
      return "whatever";
    }
  }

So my question, if an If statement is once seen as TRUE, how can an Else statement render it FALSE?


#2

In the first solution, your code keeps iterating through the contacts until both of the if statement conditions are true. so using profileLookup(“Kristian”, “lastName”), it will finally find a match in the last element where the contact’s firstName = “Kristian” and the property “lastName” exists. It then returns “Vos” which is the correct answer.

In the second solution (with the same call profileLookup(“Kristian”, “lastName”), your code only makes one iteration, because since the first contact’s firstName (“Akira”) is not equal to “Kristian”, the else block is executed and returns “whatever”.