Finding that someone special


#1


https://www.codecademy.com/courses/building-an-address-book/0/5?curriculum_id=506324b3a7dffd00020bf661


The code reads out "Bob Jones" and "Mary Jones". I have no idea how it got Mary Jones. Can someone enlighten me on this and tell me where I went wrong? Thanks.


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 (contacts[i].lastName = lastName) {
            printPerson (contacts[i]);
            }
        };
    };

search ("Jones");


#2

Your condition should returns boolean value, but you assigned for contacts[i].lastName surname "Jones". So, you have

Bob Jones
Mary Jones

as a result.


#3

change this line:

if (contacts[i].lastName = lastName)

to

if (contacts[i].lastName === lastName)

= turns it into what you want it to become.
=== asks does it match this, or is it equivalent to this? If so execute...


#4

I have tried it and it works. Thanks for your help!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.