[英]User input - Javascript Node js
I'm new to js programming and to stackoverflow, I have a school homework in which i have to make a parser for vcards (the parser works fine, I can extract all the information I need) but in this parser I have to check if the contact(s) have more then two phone numbers and if they do they must choose at most 2 phone numbers. 我是js编程和stackoverflow的新手,我有一个学校作业,其中我必须为vcard制作一个解析器(解析器工作正常,我可以提取所需的所有信息),但是在此解析器中,我必须检查是否该联系人有两个以上的电话号码,如果有,他们必须最多选择两个电话号码。 for this I made this 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 : the phoneOf() and phoneTypeOf() returns arrays with the phone numbers and their types for each contact PS:phoneOf()和phoneTypeOf()返回包含每个联系人的电话号码及其类型的数组
basically this function gives me this for a sample vcard with multiple contacts and two of them have more than 2 phones numbers: (I need 10 reputation to post images please check this link here ) 基本上,此功能为我提供了具有多个联系人的示例vcard的示例,其中两个具有两个以上的电话号码:(我需要10个信誉才能发布图片,请在此处检查此链接)
and the user must input a number in this case the variable "u", and i can't figure it out. 在这种情况下,用户必须输入一个数字“ 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
})
}
}
}
Take in count that readline is unstable and is open to changes.. 认为readline不稳定并且容易发生变化。
Here is the documentation of readline 这是readline的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.