用户input – Javascript节点js

我是新来js编程和stackoverflow,我有一个学校作业,我必须做一个parsing器vcards(parsing器工作正常,我可以提取所有我需要的信息),但在这个parsing器,我必须检查该联系人有两个以上的电话号码,如果他们这样做,他们必须select最多两个电话号码。 为此我做了这个function:

function checkPhone() { //compter le nombre de numéro par contact j for (i = 1; i < nbrContact; i++) { var count = 0; //count phone numbers for one contact for (u = 0; u < nbrPhones; u++) { if (phoneOf(i)[u] != undefined) { count++; } } //if a contact has more then 2 phone numbers if (count > 2) { var question1 = "There are " + count + " phone numbers for the contact " + "\"" + fullName[i] + "\"" + ", Please choose the two you want to keep"; console.log(question1); //print the phone numbers and their type for each contact for (u = 0; u < count; u++) { console.log(u + " " + phoneOf(i)[u] + " : " + phoneTypeOf(i)[u]); } } } 

}

PS:phoneOf()和phoneTypeOf()返回包含每个联系人的电话号码及其types的数组

基本上这个函数给了我一个有多个联系人的示例vcard,其中两个有两个以上的电话号码:(我需要10个声望来发布图片,请点击这里查看链接)

用户必须input一个数字,在这种情况下,variables“u”,我无法弄清楚。

 var readline = require('readline'); //add this ... var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); function checkPhone() { //compter le nombre de numéro par contact j for (i = 1; i < nbrContact; i++) { var count = 0; //count phone numbers for one contact for (u = 0; u < nbrPhones; u++) { if (phoneOf(i)[u] != undefined) { count++; } } //if a contact has more then 2 phone numbers if (count > 2) { var question1 = "There are " + count + " phone numbers for the contact " + "\"" + fullName[i] + "\"" + ", Please choose the two you want to keep"; console.log(question1); //print the phone numbers and their type for each contact for (u = 0; u < count; u++) { console.log(u) + " " + phoneOf(i)[u] + " : " + phoneTypeOf(i)[u]); } rl.question('wich numbers would you like to keep?:', function(numbers){ //parse the numbers maybe with an input 0,1 to keep number 0 and 1... rl.close(); //is important to close it }) } } } 

记住readline是不稳定的,可以随时更改

这里是readline的文档