Search function gives to many answers!?


#1



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


I don't get any error messages, but instead of only logging Bob Jones who is the only person with the lastName Jones it keeps logging Bob Jones and Mary Jones. So I don't know what I did wrong, however it doesn't seem right at all.....
Thank you for helping me!


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) {
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].lastName = lastName) {
            printPerson(contacts[i])
        }
        else {
            console.log("Did not match")
            }
    }
}
search("Jones");.


#2

this line:

if (contacts[i].lastName = lastName)

you assign (using single equal sign) lastName to contacts[i].lastName, which means the if condition is now a string, strings are considered true, so now it will log all contacts. Try using two equal signs (two equal sings are for comparison)


#3

Thank you for helping me out!