循环中没有保存的整数/部分工作的JS

时间:2015-12-18 15:18:23

标签: javascript node.js loops readline

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

我遇到的问题是,它确实显示购买商品时已删除金额。返回菜单并在之后检查剩余信用额时,它将返回原始金额。

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

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

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

我将非常感谢任何可以给予的帮助,因为我花了很长时间没有找到解决方案。

2 个答案:

答案 0 :(得分:1)

问题是您在购买产品后从不更新信用 下面这一行所做的就是他们对你做了多少工作吧:))

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)

答案 1 :(得分:1)

我稍微清理了你的代码并修复了你观察到的问题:

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. 从价目表中拆分产品列表有利于减少代码的WETness,然后使用array.map函数重建原始列表。

    < / LI>

    以下是其中的细分:

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

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

    1. 我更改了您的console.log语句以使用内联格式化,使其更清晰。 %s表示在此处输入字符串,%d表示在此处输入一个整数。然后使用以下参数填写这些格式说明符。

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

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