I am currently working on a menu system using an array and if statements for each option. However, I have encountered an issue where selecting certain options in a specific order does not produce the desired result.
For instance, when adding credit followed by trying to view the current credit, it fails to display the correct information.
Any assistance or suggestions would be greatly appreciated!
Best Regards.
var readlineSync = require('readline-sync')
var currentCredit = 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 select an option');
if (index == [1]) {
console.log("Your current credit balance is: £", currentCredit);
index = readlineSync.keyInSelect(menu, 'Please select an option');
}
if (index == [2]) {
var addedCredit = readlineSync.questionInt('How much credit would you like to add? ');
currentCredit += addedCredit;
console.log("Your total credit balance now is: £" + currentCredit);
index = readlineSync.keyInSelect(menu, 'Please select an option');
}
if (index == [3]) {
var removedCredits = readlineSync.questionInt('How much credit do you want to remove? ');
currentCredit -= removedCredits;
console.log("The chosen credits have been removed. Your remaining available credit is: £" + currentCredit);
index = readlineSync.keyInSelect(menu, 'Please select an option');
}