用户输入 - Javascript Node js

时间:2014-12-01 20:17:30

标签: javascript node.js input console

我是js编程和stackoverflow的新手,我有一个学校作业,我必须为vcards做一个解析器(解析器工作正常,我可以提取我需要的所有信息)但是在这个解析器中我必须检查联系人是否有超过两个电话号码,如果有,他们必须选择最多2个电话号码。为此我做了这个功能:

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()返回包含每个联系人的电话号码及其类型的数组

基本上这个功能为我提供了一个带有多个联系人的示例vcard,其中两个有超过2个电话号码:(我需要10个声望才能发布图片请查看此链接here

并且用户必须在这种情况下输入一个数字变量&#34; u&#34;,我无法弄明白。

1 个答案:

答案 0 :(得分:0)

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不稳定并且可以接受更改。

以下是documentation of readline

相关问题