jeudi 17 décembre 2015

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

Aucun commentaire:

Enregistrer un commentaire