integer未保存在循环中/部分工作的JS中

integer not saving within a loop/partially working JS

本文关键字:工作 JS 保存 存在 循环 integer      更新时间:2023-09-26

我是javascript新手,目前正在编写一个在终端中运行的基本文件。大部分都在发挥作用。它允许用户添加信用/删除信用并检查他们的信用。最后一部分是允许他们购买产品,并从他们的整体信用中扣除该金额。

我的问题是,虽然它确实显示在购买物品时金额已被删除。当回到菜单并检查剩余的信用额度时,它会恢复到原来的金额。

我尝试使用下面的代码来保存信用(即使在检查了可用信用后也会这样做):

    credit -= readlineSync.keyInSelect(products, 'What product would you like?');

但是,因为数组使用用户输入的数字,所以它将从整体信用中删除该数字,而不是实际价格。因此,例如,如果用户选择选项4,它会从信用卡中删除4,而实际上糖果是3英镑。

任何可以提供的帮助都将不胜感激,因为我已经花了很长时间没有找到解决方案。

我稍微清理了一下您的代码,并修复了您观察到的问题:

var readlineSync = require('readline-sync');
var credit = 0;
var index;
var menu = [
    'Purchase a product',
    'View your credit',
    'Add credit',
    'Retrieve a refund',
    'Quit!'
];
var products = [
    'Drink',
    'Crisps',
    'Chocolate',
    'Candy'
];
var prices = [1, 1, 2, 3];
var productList = products.map(function(product, i) {
    return product + ': £' + prices[i];
});
do {
    index = readlineSync.keyInSelect(menu, 'Please choose your option');
    if (index == 1) {
        console.log('The total amount of credit you have is: £%d', credit);
    }
    if (index == 2) {
        credit += readlineSync.questionInt('How much credit would you to purchase? ');
        console.log('The total amount of credit you have is: £%d', credit);
    }
    if (index == 3) {
        credit -= readlineSync.questionInt('How much credit would you like to remove? ');
        console.log('This credit has now been removed, your total available credit is: £%d', credit);
    }
    if (index == 0) {
        index = readlineSync.keyInSelect(productList, 'What product would you like?');
        if (index >= 0 && index < products.length) {
            credit -= prices[index];
            console.log('Thank you, your %s has now been dispensed. Your total credit is now £%d', products[index], credit);
        }
        continue;
    }
} while(index != 4);

这里有几件关键的事情,我将解决我看到的一些你没有指出的问题:

  1. 您从未声明index。虽然这在非严格模式下被认为是有效的,但这是一种糟糕的做法,因为分配未声明的变量会将其暴露在全局范围内。

  2. 你没有prices的数组,所以你当然会遇到从信用中减去什么值的问题。我冒昧地为您添加了那个数组。

  3. 将产品列表从价格列表中分离出来可以减少代码的WET,然后我使用array.map函数重建了原始列表。

以下是其明细:

var productList = products.map(function(product, i) {
    return product + ': £' + prices[i];
});

此函数遍历products中的值,对于每个值product和每个索引i,它返回一个product: £price格式的新字符串,并将其存储到数组productList的相应索引中。

  1. 我将您的console.log语句更改为使用内联格式以使其更清晰。%s的意思是在这里放一个字符串,%d的意思是放一个整数。然后使用以下参数来填充这些格式说明符。

  2. 我在您的最后一个if块中添加了一条continue语句,以便尽早返回循环。这是因为index被您选择的产品覆盖,因此它不再反映原始菜单选择。

如果您对我的反馈有任何疑问,请随时发表评论。

问题是,在他们购买产品后,你永远不会更新信用下面这行所做的就是他们会得到多少——你做对了:)

console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + (credit - removeCredit - 3));

解决方案很简单,更新信用,然后显示它们,例如:

if (index == 3) {
    credit = credit - 4;
    console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + credit);
}

所以你的代码应该看起来像

 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";
 menu[4] = "Quit!";
 products = [];
 products[0] = "Drink: £1";
 products[1] = "Crisps: £1";
 products[2] = "Chocolate: £2";
 products[3] = "Candy: £3";
 do {
     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) {
         credit -= readlineSync.questionInt('How much credit would you like to remove? ');
         console.log("This credit has now been removed, your total available credit is: £" + credit);
     }
     if (index == 0) {
         index = readlineSync.keyInSelect(products, 'What product would you like?');
         if (index == 0) {
            credit = credit - 1;
            console.log('Thank you, your Drink has now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 1) {
            credit = credit - 1;
            console.log('Thank you, your Crisps have now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 2) {
            credit = credit - 2;
            console.log('Thank you, your Chocolate has now been dispensed. Your total credit is now £' + credit);
         }
         if (index == 3) {
            credit = credit - 3;
            console.log('Thank you, your Candy has now been dispensed. Your total credit is now £' + credit);
         }
     }
 } while (index != 4)