Did you remember to list after adding a contact


#1

In the last lesson in this project I have to admit I was pretty clueless or shall we say uninspired to create the add Function. I found an "answer" in a forum post and tried it out. Like the poster I was curious as to what was going on here, but I didn't get as far as he did.

One thing that has been confusing to me from the start is that at the beginning of the project we were asked to create a function called printPerson that would take the parameter person. It seems like once or twice I had to change that parameter to contacts which is the name of the array to get things to work. I'm missing something there an I'd like to know what.

But this time I left that alone. I also apologize for adding a little something to my Search function. Maybe that's throwing a monkey wrench into things. Anyway the errors are:
Bob Jones
that person is not on the list
ReferenceError: person is not defined

And here's the code:

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(person[i]);
}
}

/*Create a search function
then call it passing "Jones"*/

function search(lastName) {

for (var i = 0; i < contacts.length; i++){
    if (lastName === contacts[i].lastName) {
        printPerson(contacts[i]);
    }

    else {
        console.log("that person is not on the list")
    }
}

}

search("Jones")

function add (firstName, lastName, email, phoneNumber)
{
contacts[contacts.length] =
{
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
}
}

add("Mark", "Madison", "mark@mark.com", "(502) 555-5555");
list();


#2

That is the ReferenceError (in the list() function) since there is no defined array, person.

var list = function () {
    for (var i = 0; i < contacts.length; i++) {
        printPerson(contacts[i]);
    }
};

The printPerson() function has a local variable, person that is not available outside of the function. This should be the only reference.

As for the search() function, it should not have an else statement. If you wish to indicate no matches found, then do it after the for statement.

var search = function (name) {
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].lastName === name) {
            printPerson(contacts[i]);
        }
    }
    console.log(name + " not found";
};