简体   繁体   中英

How to create a list of contacts using javascript?

I've got to create a list of contacts using javascript which allows the user to enter contacts, and so that the program runs until the user quits. I'm a little lost as to whether I need to add alerts or not. Here's some of my code below, it's a work in progress so there are no expected console outcomes from it as yet.

/* 
Activity: Contact manager
*/

// TODO: Complete the program
var Contact = {
// initialise contacts
init: function(last_name, first_name) {
    this.last_name = last_name;
    this.first_name = first_name;
},
describe: function () {
    var description = this.last_name + "," + this.first_name;
    return description
},
// add a contact
contact_list: function () {
    var list_of_contacts = []

    }   
}

var contact1 = Object.create(Contact);
contact1.init("Smith", "John");

var contact2 = Object.create(Contact);
contact2.init("Doe", "Jane");

var contact_list = [];
contact_list.push(contact1);
contact_list.push(contact2);

This is what the console should look like, if the exercise is completed successfully:

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

Upon providing the option 1 :

Here's the list of all your contacts:
Last name: Smith, first name: John

The rest is almost identical.

You already have a list, you are pusing them to the array: contact_list.push(contact1);

In order to log output a list, all you have to do is loop the array and print each name;

let output = "contacts: ";
for(let i=0;i< contact_list.length; i++){
    output+= "Last name:" + contact_list[i].last_name  + ", first name:" +contact_list[i].first_name ;
}
console.log(output);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM