Question 5/6


#1
 <


var search = function (lastName) {
var contactsL = contacts.length;
for (var i = 0; i < contactsL; i++) {
if(lastName === contacts[i].lastName) {
printPerson(contacts[i]);
};
};
};
search("Jones");>
When I do this, it turns out fine. However when I do this,
var search = function (lastName) {
var contactsL = contacts.length;
for (var i = 0; i < contactsL; i++) {
if(contacts[i].lastName === lastName) {
printPerson(contacts[i]);
};
};
};
search("Jones");
It doesn't work. Could someone please give me an explanation?

#2

@captaincanadia,
I ran your code with following Data, and I had no problem.....
It makes no difference to the comparison if you
use (lastName === contacts[i].lastName)
or (contacts[i].lastName === lastName)


var friends=    { bill: 
       { firstName: 'Bill',
         lastName: 'Gates',
         number: '444-444-4444',
         address: [ 'One Microsoft way', 'Redwood', 'WA' ] },
      steve: 
       { firstName: 'Steve',
         lastName: 'Jones',
         number: '333-333-3333',
         address: [ 'Two infinite loop', 'Bentown', 'DV' ] } };

var contacts =[friends.bill,friends.steve];

var printPerson =function(anObject) {
    console.log( anObject);
};

console.log( contacts);

var search = function (lastName) {
    var contactsL = contacts.length;
    for (var i = 0; i < contactsL; i++) {
        if(lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }
    }
};
search("Jones");

//When I do this, it turns out fine. However when I do this,


var search = function (lastName) {
    var contactsL = contacts.length;
    for (var i = 0; i < contactsL; i++) {
        if(contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        }
    }
};
search("Jones");