integer未保存在循环中/部分工作的JS中
integer not saving within a loop/partially working JS
我是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);
这里有几件关键的事情,我将解决我看到的一些你没有指出的问题:
您从未声明
index
。虽然这在非严格模式下被认为是有效的,但这是一种糟糕的做法,因为分配未声明的变量会将其暴露在全局范围内。你没有
prices
的数组,所以你当然会遇到从信用中减去什么值的问题。我冒昧地为您添加了那个数组。将产品列表从价格列表中分离出来可以减少代码的WET,然后我使用array.map函数重建了原始列表。
以下是其明细:
var productList = products.map(function(product, i) {
return product + ': £' + prices[i];
});
此函数遍历products
中的值,对于每个值product
和每个索引i
,它返回一个product: £price
格式的新字符串,并将其存储到数组productList
的相应索引中。
我将您的
console.log
语句更改为使用内联格式以使其更清晰。%s
的意思是在这里放一个字符串,%d
的意思是放一个整数。然后使用以下参数来填充这些格式说明符。我在您的最后一个
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)
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- JS可以在Chrome中工作,但不能在Firefox中工作
- JS,用于播放提示音以通知未按预期工作
- JS在firefox中无法正常工作
- 为什么不是'我的JS滑块正在工作
- 谷歌地图Api和JS代码不工作
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 当加载几个js文件时,defer属性应该如何工作
- 这和javascript中的闭包(在纸上工作.js)
- 嘶嘶声中的正则表达式如何工作.js
- 第一个现实生活-工作JS脚本.我怎样才能使它更有效率
- 简单事件侦听器不工作-JS
- 为什么不't工作js调试智能
- 字符串模板不工作- js
- 多维数组indexOf不工作js
- 原型“this"不工作(JS)
- 不能让Autobahn的例子工作(js和python)
- 为什么这个选择器不能工作?JS
- 为什么这个脚本不能工作?(JS)
- Spring MVC 和 Bootstrap - css,img 工作.js不工作