Finding That Special Someone


#1

So my code passed and I'm on the next section now, but it prints out both Bob and Mary's names. Isn't it just supposed to print Bob's name? Or maybe I'm reading it 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 if statement:

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

has an unnecessary semi-colon, and the curly brackets are missing. This causes printPerson to be always reached


#3

Thank you!! Have fixed it now :slight_smile:


#4

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