Javascript 变量问题

标签 javascript variables

您好,我正在尝试让这个程序添加整个循环中累积的所有总成本。但该程序会继续打印最新的成本,而不是所有已“购买”的产品。这是我的代码:`

        var Customer_Name, Discount, Product="", Price, Quantity="", Cost ;
        var blanket_price = 25;
        var lotion_price = 60;
        var surfboard_price = 60;
        var sunscreen_price = 60;
        var Customer_Name = prompt("Welcome to Jersey Shore Inc.! What is your name?","Type Name Here");
        alert("Hello "+Customer_Name+", Please look through all available products and services before placing your order.");


    do
    {


    Product= prompt("What product would you like?","Type Product Name Here");
    Quantity= prompt("How much of the "+Product+" would you like?","Type Quantity Here");
    var Total_Cost;
    var SumOrderTotal=0;

         if (Product == "blanket")
            {
                alert("You received a 50% discount! Click okay to see your receipt below.");
                Total_Cost= 0.5*(Quantity*blanket_price);
                Cost=(Quantity*blanket_price);
                Discount = .50*blanket_price;
            }
            else if (Product == "lotion")
            {
                alert("You received a 50% discount! Click okay to see your receipt below.");
                Total_Cost= 0.5*(Quantity*lotion_price);
                Cost=(Quantity*lotion_price);
                Discount = .50*lotion_price;
            }
            else if (Product == "surfboard")
            {
                alert("You received a 50% discount!Click okay to see your receipt below.");
                Total_Cost= 0.5*(Quantity*surfboard_price);
                Cost=(Quantity*surfboard_price);
                Discount = .50*surfboard_price;

            }
            else if (Product == "sunscreen")
            {
                alert("You received a 50% discount! Click okay to see your receipt below.");
                Total_Cost= 0.5*(Quantity*sunscreen_price);
                Cost=(Quantity*sunscreen_price);
                Discount = .50*sunscreen_price;

            }

             if((Product=="blanket" || Product=="sunscreen" || Product=="lotion" || Product=="surfboard"))
            {

                document.write("The cost of buying " + Quantity+ " of " + Product + " is $ "+ Cost +". </br>");
                document.write("The discount for this purchase is $ "+Total_Cost+". <br/>");



            }
            else 
            {
                alert("Sorry "+Customer_Name+" you entered an invalid product.(Refer to table for our products) Please Refresh the page to reload and place a new order.");

            }
     var SumOrderTotal = Total_Cost + SumOrderTotal;
     var user_answer=confirm("Would you like to continue purchasing products?");  

    }
    while(user_answer==true);

                document.write("Thank you for placing an order with us, " +Customer_Name+". <br/>");
                document.write("Your total order cost is $"+ SumOrderTotal+ ". <br/>");

`

最佳答案

SumOrderTotal 移到 do...while 之外。

像这样:

  var SumOrderTotal=0;
  do {      
    Product = prompt("...");
    Quantity = prompt("...");
    var Total_Cost;
  } while (...) {
  ...
  }

因为每次开始循环时都会初始化为 0(do..while)。

关于Javascript 变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15698911/

相关文章:

Javascript 字符串变量未加引号?

c - 在结构体中组织变量是否有任何陷阱

javascript - 在 jquery 中递增并存储 var

javascript - 如何 chop 字符串并删除尾随字符(如果存在)?

javascript - 您不能接受使用此 API 的付款,因为印度不再支持它 Next js、Strapi、Stripe

javascript - 在 fetch React-Native 中返回代码

javascript - 循环遍历数组时出现问题,避免添加 id

C++ 常量字符

C++ 字符串变量声明

java - 似乎无法将 int 值从 Main 返回到 Player Class