5. Finding that special someone - passed, but weird output


#1

Building an Address Book - #5 - Finding that special someone

Hey, I seem to have passed this part, but the words being logged to the console aren't quite right. It's printing out "Bob Jones" and "Mary Jones" when I submit the code. It seemed to satisfy the check for passing, but obviously I've messed something up anyway. Any ideas?

Thanks

var bob = {
    firstName: "Bob",
    lastName: "Jo23es",
    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

When you do
printPerson(contacts[i]);
Change the i to 0


#3

Add break; after printPerson(contacts[i]); .


#4

if(contacts[i].lastName = lastName)

Remember that "=" is for declaring values, "===" is for comparing.


#5

Ah! Thanks, there's the issue. Gamemaker Studio has created some bad habits when it comes to = vs ===. Thanks a lot!


#6

No problem, having used Gamemaker and now working a lot in Twine 2, I feel your pain!


#7

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