整数不保存在一个循环/部分工作的JS

我是新来的JavaScript,目前正在写一个terminal运行的基本文件。 它的大部分工作。 它允许用户添加信用/删除信用和检查他们的信用。 最后一部分是允许他们购买一种产品,并从总体积分中扣除这一数额。

我所遇到的问题是,虽然它显示购买物品时,金额已被删除。 返回菜单并检查剩余的信用额度后返回原始金额。

我已经尝试使用下面的代码,而不是保存信用(甚至在检查可用信用之后):

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

但是因为数组使用用户input的数字,所以从整体信用中删除该数字而不是实际的价格。 因此,例如,如果用户select选项4,则当实际上糖果是£3时,从信用中移除4。

任何可以给予的帮助将不胜感激,因为我花了相当长的时间没有find解决办法。

我清理了一下你的代码,并解决了你观察到的问题:

 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 。 虽然这在非严格模式下被认为是有效的,但这是不好的做法,因为分配一个未声明的variables会将其暴露给全局范围。

  2. 你没有一个prices的数组,所以当然你会遇到问题,找出什么价值减去你的信用。 我冒昧地为你添加这个数组。

  3. 从价目表中拆分产品列表有利于降低代码的湿度 ,然后使用array.map函数重build原始列表。

这是细分:

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

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

  1. 我改变了你的console.log语句,使用内联格式来使它更清洁。 %s表示在这里放一个string, %d表示在这里放一个整数。 然后使用以下参数来填充这些格式说明符。

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

如果您对我的反馈有任何疑问,欢迎发表评论。

问题是,你从来没有更新信贷后,他们购买了一个产品,所有以下行是他们会有多less,你做了正确的:)

 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)