5 finding that special someone


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}

/*Create a search function
then call it passing "Jones"*/
var search = function (lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) if (lastName = contacts[i].lastName) {
printPerson(contacts[i]);}
}

search("Jones")

I am passing the exercise and getting the output Bob Jones, but I am also getting Mary Johnson printed at the same time? Is this just codecademy checking my search function?


#2

this line:

f (lastName = contacts[i].lastName)

you use a single equal sign (=) which means assign value, you should compare here (two equal signs)

It also explains why you see both names, since strings are considered true:

if ("test"){
  console.log(true);
}

the printPerson function gets called both times. If you compare, only Jones shows