I keep getting an error and don't see the problem


#1

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]);
	}
}
var search = function(lastName)
{
    var contactsLength = contacts.length;
    for (var i = 0; i < contacts.length; i++)
    {
        if (lastName === contacts[i].lastname)
        {
           printPerson(contacts[i]);
        }
        else 
        {
            return console.log("The name does not appear in the data base");
        }
    }
};
search("Jones");

After I click save&Submit it says "Oops, try again. It looks like your search function didn't log "Bob Jones" to the console. Did you remember to call search after you defined it?"
Please help. I'm sure I'm missing some kind of punctuation mark but I can't see it


#2

As the code get's longer please make use of the formatting options as plain text code is awful to read.

Guess at least one problem is the if/else here:

    if (lastName === contacts[i].lastname)
    {
       printPerson(contacts[i]);
    }
    else 
    {
        return console.log("The name does not appear in the data base");
    }

The if is ok and necessary, but the else makes sure that at least on of the cases is chosen. Meaning in this case you either get the name you wanted or you provide the "not found"-message and leave the function (on the first entry of your array). Also are you asked to use console.log or return here?

Anyway return console.log is a bad choice as return passes a value to the caller but console.log has no value.


#3

Change:
if (lastName === contacts[i].lastname) // lastname ==> lastName
To:
if (lastName === contacts[i].lastName)


#4

Change:
if (lastName === contacts[i].lastname)
{
printPerson(contacts[i]);

To:
if (lastname === contacts[i].lastName)
{
printPerson(contacts[i]);
break; // to break out of the loop. Not perfect but works for this example.