Didn't add a person object #6 We made a friend


#1


6 - We made a friend


So, I wrote the code out, I run it, I actually get the person I intend to add, but I also get a line that says "undefined undefined", and I get an error message that I did not add a person even though when I run list() I do, in fact, add a person. I guess I just need help understanding what I did wrong, when I take add out of the contacts array I get an error message that there are only 2 items in contacts, so I'm stumped.


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, add];

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]);
	}
}

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

add("Biff", "JoeBob", "breal@glah.net", "(555)555-5555");
list();


#2

Giggity yay! I figured it out, I just removed add and it worked! So let this code be a shining example of... something. I dunno.


#3

add is a function and doesn't have the properties you were printing out, so you get undefined.

Sane languages would tell you that you're accessing something that isn't there, but not JS.


#4

Thanks for your reply, that's the kind of stuff I need, I don't understand how things that SHOULD make sense in my mind end up not working, and there's always this "Well dang" moment where I have no idea what to do next.