简体   繁体   English

如何使用javascript创建联系人列表?

[英]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. 我必须使用javascript创建联系人列表,该列表允许用户输入联系人,以便程序运行直到用户退出。 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 : 提供选项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); 您已经有一个列表,正在将它们插入数组: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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