用户输入 - Javascript Node js.

User input - Javascript Node js

本文关键字:Node js Javascript 输入 用户      更新时间:2023-09-26

我是js编程和stackoverflow的新手,我有一个学校的家庭作业,我必须为vcard制作一个解析器(解析器工作正常,我可以提取我需要的所有信息(,但是在这个解析器中,我必须检查联系人是否有两个以上的电话号码,如果他们有,他们必须选择最多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个声誉才能发布图像,请在此处查看此链接(

在这种情况下,用户必须输入一个数字,变量"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
            })
        }
    }
}

计数读数行不稳定并且对更改持开放态度。

这是阅读线的文档