【发布时间】:2016-07-20 02:56:06
【问题描述】:
我使用此代码的目标是创建一个临时购物车,让您可以选择从一小部分商品中进行选择、选择数量、获取总数、删除商品(如果需要)并继续“结帐”。我的问题是,一旦我通过项目类型(通过警报)获得价格结果,我不知道如何将所有潜在警报添加在一起,因为它在 for 循环中运行。有谁知道如何做到这一点?我对JS相当陌生,不知道如何进行。这也是我在堆栈上的第一个问题,如果这是一个奇怪的问题,请原谅我。
这是我的代码:
var item = function(itemName, itemPrice, itemTax) {
this.products = itemName;
this.cost = itemPrice;
this.taxes = itemTax;
this.total = itemPrice + (itemPrice * itemTax)
}
var list = [];
list[0] = new item("Shoes", 67.99, .075);
list[1] = new item("Coat", 78.99, .075);
list[2] = new item("Book", 9.99, .075);
list[3] = new item("Suitcase", 56.99, .075);
function theStore() {
var greeting = prompt("Welcome to xxxxxxxx! Do you want to begin shopping?");
while (greeting === "Yes")
{
for (var i = 0; i < list.length; i++)
{
var j = list[i];
var adding = prompt("The " + j.products + " cost " + j.cost + ". Would you like to add it to your cart?")
if (adding === "Yes")
{
var addingMore = prompt("How much do you want?")
if (addingMore < 1)
{
alert("This item was not added to your cart.")
}
else
{
alert(addingMore + " of " + j.products +" has been added to your cart." + " Your total for this kind of item is " + addingMore * (j.cost) + ".")
}
}
else
{
alert(j.products + " was not added to your cart.")
}
}
greeting = prompt("Do you want to continue shopping? Yes or No");
}
}
theStore()
理想情况下,当用户提示“否”(第 47 行)时,我想向他们显示所有项目的总数。
感谢任何帮助/提示/建议。
【问题讨论】:
标签: javascript for-loop while-loop alert