Code does not work and I don't know why


#1

Hi,
I have a problem. I'm working on the We Made A Friend code, but it won't work. My 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 add = function (firstName, lastName, email, phoneNumber) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.email = email;
    
    contacts[contacts.length] = Object;
};

add("myFirstName", "myLastName", "my@mail.com", "xxxxxxxxxxx");


var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

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

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName) {
    var i;
    var contactsLength = contacts.length;
    
    for (i = 0; i < contactsLength; i++) {
        if (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }
    }
};

list();

the error:

Oops, try again. Make sure the contacts array still exists. Your code threw the following error when checking contacts: TypeError: Unable to get property 'length' of undefined or null reference

Can anyone help please? :slight_smile:


#2

your add function is before your contacts array, so your add function tries to push something to an array which is yet undefined


#3

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