5. Finding That Special Someone: Printing Mary Jones?


#1


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

Hey all,

The exercise is saying that I've completed it correctly, but instead of printing only "Bob Jones" like it's supposed to, it prints both "Bob Jones" and "Mary Jones" to the console when I execute. I need help identifying the error that I've committed that would cause it to print "Mary Jones", especially because Mary Jones isn't part of the array! Haha.

Thanks in advance!

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

search("Jones");


#2

I'm not completely sure, but at the end you said

Mary had Jones in her last name so it only gave you the bit of her name that had Jones in it.

Please note that this is just a hunch, I'm not 100% sure but I'm pretty sure :slight_smile:


#3

No, that's not it... I tried changing Mary's contact info (changed bot the lastName parameter and the email, just to make sure there was nothing even similar to "Jones" in her name) and it still printed, "Mary Jones" underneath "Bob Jones". :frowning:

Thanks for trying to help though!


#4

That's weird :confused: Have you tried refreshing the page or quitting it and then going back on? If you did those then I'll revert to the default answer and say that it's probably a bug.


#5

Yeah, I've even tried it on another browser, haha!

I guess we'll just have to assume it's a bug unless someone else can figure it out. Thanks for the help!


#6

I don't feel like a was much help, but I'll take what I can get and say your welcome :slight_smile:


#7

Try this: 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");

Notice that you have contacts[i].lastName=lastName instead of "==="


#8

Ah! Solved! Thank you so much. That makes sense. I've got to remember the difference between "=" and "==="!


#9

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