[HELP]Building an Address book turned into code battle o.o


#1

Im totally confused :confused:

Here's my little 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(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 (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }
	}
};


var add = function (firstName, lastName, email, phoneNumber) {
    var shyam = new Object();
    shyam.firstName = firstName;
    shyam.lastName = lastName;
    shyam.email = email;
    shyam.phoneNumber = phoneNumber;
    contacts[contacts.length] = object
};

add('Shyam', 'Kiran', 'shyamkiranrocks@gamil.com', '(+91) 328394234');

printPerson(contacts.shyam)

I get this error message

Oops, try again. There was a problem with your syntax.

How do I get through this? and please explain me


#2

one of the errors is here

contacts[contacts.length] = object

object is not defined

for the add function try this.

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

#3

Not working yet :confused:

Here's my edited code

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

add('Shyam', 'Kiran', 'shyamkiranrocks@gamil.com', '328394234');

#4

now call

list(contacts);

remember add contact details exactly as they wanted it

add("Ant","Crint","crint@hotmail.com","5656565656555");