如何在用户输入" no"

时间:2017-01-08 22:50:23

标签: javascript command-line-interface

我试图通过CLI向用户提出几个问题,当遇到下面的响应函数中的问题时,我遇到了一个小问题。#34; No"产品选择功能仍然继续并打印出msg。

我意识到这是因为如果" No"到目前为止在列表中的响应,但是我不确定将它放在何处,因为当我之前将它放在代码之上时它也没有用。

readline = require('readline-sync');

var rl = require('readline');

var prompts = rl.createInterface(process.stdin, process.stdout);

prompts.question("Hi, are you ready to order? (Yes/No) ", function(response) { //This will be the initial display until the customer engages.

var msg = "";

if (response === "Yes") {

    msg = "Great, first lets check your balance";

    console.log(msg);
    process.exit();

}
});

prompts.question("Your balance is currently £0.00, would you like to add credit? (Yes/No) ", function(credit) {

var msg ="";

if(credit === "Yes") {

    var addCredit = "OK, please enter the amount you would like to add. (Format £0.00)";

    bill.question("")

}
else if(credit === "No") {
    msg = "No problem, you will now be taken back to the main menu.";
}

if(addCredit = ("OK, please enter the amount you would like to add. (Format £0.00)")) {

    prompts.question("")

}
})

2 个答案:

答案 0 :(得分:2)

您必须存储从prompt方法返回的值。

使用:

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    border: 1px solid #ccc;
    white-space: nowrap;
}

//编辑:

我不明白你为什么要将变量(答案)传递给那个函数

我会这样写:

var answer = prompt("Hi, are you ready to order? Type Yes or No");

答案 1 :(得分:0)

试试这个。问题在于嵌套条件的方式。对于第二个提示,我建议您使用switch语句。只是这样您的使用才能获得默认消息。

function getCustomerOrder() {
  prompts.question("Hi, are you ready to order? (Yes/No)", function(response) {
    var msg = "";

    if (response === "Yes") {
      msg = "Great, lets get into it!";
      console.log(msg);
      getPurchase();

    }
    else if (response === "No") {
      msg = "No problem, take your time.";
      console.log(msg);
      process.exit(1);
    }
  });
};


function getPurchase() {
  prompts.question("What would you like to purchase? (Please type exact name of Product) ", function(productchosen) {
    var msg = "";

    if (productchosen === "Orange Lucozade") {
      msg = "Orange Lucozade, good choice! That will be £1.00";
    }
    else if (productchosen === "Original Lucozade") {
      msg = "Original Lucozade, good choice! That will be £1.00";
    }
    else if (productchosen === "Apple Lucozade") {
      msg = "Apple Lucozade, good choice! That will be £1.00";
    }

    console.log(msg);
  });
};

// getCustomerOrder();