I want to create a contact manager in javascript. It will contain two initial contacts: John Smith and Jane Doe. Each contact should have a first and last name. Also user must be able to create a new contact. Contacts must be added in array. Program will be with switch structure.

Ive created the program and all is Ok to the part, that after creating the new contact and adding it to the array of people, then i cant list the new contact, with choosing number 1 (For list all contacts). Can anybody help me.

Here is my code:

/* 
Activity: Contact manager
*/

// TODO: Complete the program

/*
Welcome to your contacts manager!
1: List contacts
2: Add a contact
0: Quit

Here`s the list of all your contacts:
Last name: Smith, first name: John
Last name: Doe, first name: Jane
*/
console.log("Welcome to your contacts manager!");

do {
    console.log("1: List contacts");
    console.log("2: Add a contact");
    console.log("0: Quit");

    var Person = {
    //initialize the personalbar
    init: function(last, first){
        this.last = last;
        this.first = first;
    },
    //Describe of peoples
    describe: function(){
        var description = "Last name: " + this.last + ", first name: " + this.first;
        return description;
    }
};

var person1 = Object.create(Person);
person1.init("Smith", "John");

var person2 = Object.create(Person);
person2.init("Doe", "Jane");

var person3;

var peoples = [];
peoples.push(person1);
peoples.push(person2);

function listPeople(){
    peoples.forEach(function(people){
        console.log(people.describe());
    });
}

function addNewPerson (last, first){
    person3 = Object.create(Person);
//  last = prompt("Enter last name for person: ");
//  first = prompt("Enter first name for person: ");
    person3.init(last, first);
    var arrayPush = peoples.push(person3);
    return arrayPush;
}

    var nmbr = prompt("Enter your digit(1, 2 or 3): ");
    var choice = parseInt(nmbr);

    switch(choice){
        case 1 : 
            console.log("Here`s the list of all your contacts: ");
            listPeople();
            break;
        case 2 : 
            last = prompt("Enter last name for person: ");
            first = prompt("Enter first name for person: ");
            addNewPerson(last, first);
            break;
        default:
            break;
    }
}
while (choice != 0);

Recommended Answers

All 3 Replies

But this is not, what i exactly want. I want to make it with my code, maybe i can`t make something with variables (local and global). :(

Then break it down to bite sized code passages you can manage. You have dozens of things to get working so break up the effort to smaller tasks. That way you can work your way up to a finished app.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.