ZenoX
ZenoX

Reputation: 117

if statement not working correctly? JS

I am currently using an array for a menu and using if statements for each option. The problem i have found is if the first option i select uses an if statement that is below an option i ask later, it doesn't give a result.

For example. If i add credit, and then the next option i select to view current credit, it does not complete that task and shows nothing?

Any help would be appreciated!

Kind Regards.

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');
}

Upvotes: 1

Views: 399

Answers (3)

MinusFour
MinusFour

Reputation: 14423

I think what you want is to loop your script:

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));
    }
}

To get out of the loop with an option:

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));
    }
}

Where 4 is the option to get out. Notice too that I'm using "loose" equality, just because I don't really know if keyInSelect returns a number or a string.

I think syntax is kind of awkward (specially assignment and comparison in while condition) so here's another version.

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)

Upvotes: 3

Mathews Mathai
Mathews Mathai

Reputation: 1707

The problem is simple enough.The previous if statements will not be considered as the control cannot move back. You should enter all these if statements in a function. Whenever a new input is taken,call this function so that each and every if statement is checked.Control moves from one line to the next and it does not move back to a previous line unless it is in a function(call it) or loop.

If you use a function,you can call the function again and each and every if statement will be checked again.

eg:

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

Control cannot back to the previous if statement.So no popup will be shown.

function alerts()
{

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

In this case an alert box with one pop up coz the function is called and therefore,the previous statements are also checked.

Upvotes: 0

Robbert
Robbert

Reputation: 713

Try to remove the brackets in your if statements, like so:

if (index == 1)

Upvotes: 0

Related Questions