Basic JavaScript: Profile Lookup problem with( && )

Why my && doesn’t work on my code, if you just put this code (contacts[i].hasOwnProperty(prop)) in a separate if the hole code will be working, so what the problem here ?
thx by the way.

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 {

    return "No such property";

 }

}

    return "No such contact";

// Only change code above this line

}

what happens if name == contacts[i].firstName is false? the else statement execute. Meaning you are always returning No such property if the first contact is not the right one

you can look at it with this to see it step by step: http://pythontutor.com/javascript.html

1 Like