Finding that Special Someone - everyone is Jones


#1

Hi,

My code below passed, but I'm not getting why does the console print both Bob Jones and Mary Jones? ...yes, Jones.

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"*/
function search(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if (contacts[i].lastName = lastName) {
printPerson(contacts[i]);
} else {
console.log("No such person is found.");
}
}
}

search("Jones"); // O_o?


#2

@betaslayer24635,
In the IF-statement of your search function
you are using

(contacts[i].lastName = lastName)

and are re-assigning the property contacts[i].lastName to the =current VALUE= of the lastName variable

What you really want is the comparison operator ===

Add the following code-lines

search("Jones");
console.log( contacts );
/*
In some sections of this course, the course-checker _Display's data_
this you could then identify by placing to EXTRA code-line's:
*/
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );