如果声明不正常? JS

时间:2015-12-17 18:46:47

标签: javascript if-statement npm

我目前正在使用数组作为菜单,并为每个选项使用if语句。我发现的问题是,如果我选择的第一个选项使用的if语句低于我后面要求的选项,它就不会给出结果。

例如。如果我添加信用,然后我选择查看当前信用的下一个选项,它不完成该任务并且什么都不显示?

任何帮助将不胜感激!

亲切的问候。

var readlineSync = require('readline-sync')
var credit = 0;
var removeCredit = 0;

menu = [];
menu[0] = "Purchase a product";
menu[1] = "View your credit";
menu[2] = "Add credit";
menu[3] = "Retrieve a refund";

index = readlineSync.keyInSelect(menu, 'Please choose your option');

if (index == [1]) {
console.log("The total amount of credit you have is: £", credit);
index = readlineSync.keyInSelect(menu, 'Please choose your option');
}

if (index == [2]) {
var credit = readlineSync.questionInt('How much credit would you like to purchase? ');
console.log("The total amount of credit you have is: £" + (credit));
index = readlineSync.keyInSelect(menu, 'Please choose your option');
}

if (index == [3]) {
var removeCredit = readlineSync.questionInt('How much credit would you like to remove? ');
console.log("This credit has now been removed, your total available credit is: £" + (credit - removeCredit));
index = readlineSync.keyInSelect(menu, 'Please choose your option');
}

3 个答案:

答案 0 :(得分:3)

我认为你想要的是循环你的脚本:

var index, credit, removeCredit;
while(true) {
    index = readlineSync.keyInSelect(menu, 'Please choose your option');
    if (index == 1) {
        console.log("The total amount of credit you have is: £", credit);
    }

    if (index == 2) {
       credit = readlineSync.questionInt('How much credit would you to purchase? ');
        console.log("The total amount of credit you have is: £" + (credit));
    }

    if (index == 3) {
       removeCredit = readlineSync.questionInt('How much credit would you like to remove? ');
       console.log("This credit has now been removed, your total available credit is: £" + (credit - removeCredit));
    }
}

使用选项退出循环:

var index, credit, removeCredit;
while(index = readlineSync.keyInSelect(menu, 'Please choose your option') != 4) {
    if (index == 1) {
        console.log("The total amount of credit you have is: £", credit);
    }

    if (index == 2) {
       credit = readlineSync.questionInt('How much credit would you to purchase? ');
        console.log("The total amount of credit you have is: £" + (credit));
    }

    if (index == 3) {
       removeCredit = readlineSync.questionInt('How much credit would you like to remove? ');
       console.log("This credit has now been removed, your total available credit is: £" + (credit - removeCredit));
    }
}

4可以选择离开。另请注意,我正在使用“松散”等式,因为我不知道keyInSelect是否返回数字或字符串。

我认为语法有点尴尬(特别是条件中的赋值和比较)所以这是另一个版本。

var index, credit, removeCredit;
do {
   index = readlineSync.keyInSelect(menu, 'Please choose your option');
   switch(index) {
      case 1:
          console.log("The total amount of credit you have is: £", credit);
      break;

      case 2:
          credit = readlineSync.questionInt('How much credit would you to purchase? ');
          console.log("The total amount of credit you have is: £" + (credit));
      break;

      case 3:
          removeCredit = readlineSync.questionInt('How much credit would you like to remove? ');
          console.log("This credit has now been removed, your total available credit is: £" + (credit - removeCredit));
      break;

   }
} while(index != 4)

答案 1 :(得分:0)

尝试删除if语句中的括号,如下所示:

if (index == 1)

答案 2 :(得分:0)

问题很简单。以前的if语句不会被视为控件无法移回。 您应该在if中输入所有这些function语句。 每当获取新输入时,调用此函数以便检查每个if语句。控件从一行移动到下一行并且它不会移回到前一行,除非它在函数中(调用它)或循环。

如果您使用某个功能,则可以再次调用该功能,并且将再次检查每个if语句。

例如:

if(i==1)
alert("one");
if(i==2)
alert("two");
 i=1;
if(i==3)
alert("three");

控件无法返回上一个if语句。因此不会显示弹出窗口。

function alerts()
{

if(i==1)
alert("one");
if(i==2)
alert("two");
 i=1;
alerts();
if(i==3)
}

在这种情况下,会弹出一个弹出警告框,因为该函数被调用,因此也会检查先前的语句。