简体   繁体   English

Javascript做时循环问题

[英]Javascript do-while looping problems

I need help with looping the javascript in a do/while statement loop. 我需要在do / while语句循环中循环javascript的帮助。

The problems I am having is that I don't want to display the incorrect information in my table when entering an invalid product, but it does display in the document.write. 我遇到的问题是,当我输入无效的产品时,我不想在表格中显示不正确的信息,但是它确实显示在document.write中。 I need help in order to make sure that the incorrect information won't be displayed. 我需要帮助以确保不会显示不正确的信息。

Also, when I hit "ok" to add more to my order, it doesn't loop it but merely displays the document.write. 另外,当我单击“确定”以向订单中添加更多内容时,它不会循环播放,而只会显示document.write。 I want it to loop if you hit the "ok" button. 如果您单击“确定”按钮,我希望它循环播放。

Thank you for the help. 感谢您的帮助。

Here is the code: 这是代码:

 <html>

    <head><title>Javascript Assignment 3: Daniel Weiner</title>
      </head>
    <body bgcolor="brown">

    <h1 align="center">Big Ben's Burgers-eCommerce</h1> 

    <table border="1" width="100%" height="450px" bgcolor="gray"><tr>
      <th align="center">Product/Service</th>
      <th align="center">Price</th>
      <th align="center">Discount</th>
    </tr>

    <tr bgcolor="orange">
      <td align="center"><font size="3">Hamburger 
    <br>
    <br>
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.256.Hamburger.html" target="_blank">Classic Hamburger</a>

      <td align="right" bgcolor="orange"><font size="3">$8.00</font></td>
      <td align="center" bgcolor="orange"><font size="3">.10</font></td>
    </tr>

    <tr bgcolor="orange">
      <td align="center"><font size="3">Cheeseburger
    <br>
    <br>
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.284.cheeseburger.html" target="_blank">Classic Cheeseburger  </a>
    </font></td>
      <td align="right" bgcolor="orange"><font size="3">$9.00</font></td>
      <td align="center" bgcolor="orange"><font size="3">.05</font></td>
    </tr>

    <tr bgcolor="orange">
      <td align="center"><font size="3">Soda
    <br>
    <br>
    <a href="http://www.mcdonalds.com/us/en/food/full_menu/beverages.html" target="_blank">Fabulous Drinks</a>
    </font></td>
      <td align="right" bgcolor="orange"><font size="3">$2.00
    </font></td>
    <td align="center" bgcolor="orange"><font size="3"> .07</font></td>
    </tr>

    <tr bgcolor="red">
      <td align="center"><font size="3"> French Fries
    <br>
    <br>
    <a href="http://www.mcdonalds.com/us/en/food/product_nutrition.snackssides.120.small-french-fries.html" target="_blank"> Fries</a>
    </font></td>
      <td align="right" bgcolor="red"><font size="3"> $4.00</font></td>
      <td align="center" bgcolor="red"><font size="3">.15</font></td>

    </table>
    <script type="text/javascript">
    /*Daniel Weiner, Fengpeng Yuan, Javascript 2, Nov 4,2011*/

    var username;
    var bprice= 8;
    var chprice= 9;
    var sprice= 2;
    var fprice= 4;
    var price= 0;
    var a;
    var b;
    var product= "hamburger, cheeseburger, soda, fries";
    var quantity =0;
    var total;
    var cost = 0;
    var discount= 0;
    do{
    username =prompt("Welcome to Big Ben's Burgers. Please enter your name.", "");
    alert("Hello " + username+". Please look through our available products and services before placing your order.","");
    product=prompt("What do you want?","");

    quantity =1*prompt("How many of " +product+ " would you like?");




    if (product == "hamburger")
    {   
    price = bprice; 

    discount = .1;
    }
    else if (product == "cheeseburger")
    {   
    price = chprice;

    discount = .05;
    }

    else if (product == "soda")
    {   
    price = sprice;

    discount = .07;
    }

    else if (product == "fries")
    {   
    price = fprice;

    discount = .15;
    }   

    else{
        alert("Sorry, " +username+  " Your item not found.");
    }
        cost=price*quantity
        discount=price*discount*quantity
        total=cost-discount
    document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>");
    document.write("This discount for this purchase is $" +discount+ ".<br/>");

    }while(a==false)
    a = confirm("Do you want to place another order?");

    (b==false)
    document.write("Thank you for placing an order with us, " +username+ ".<br/>");
    document.write("The total order cost is $" +total+ ".");

    </script>
    </body>
    </html>

对于问题的第二部分,您必须在循环中使用下面的语句

 a = confirm("Do you want to place another order?");

It looks like you aren't conditioning the part that outputs the result on whether or not a valid product was selected. 似乎您没有在根据是否选择了有效产品来调节输出结果的零件。 You need something like the following; 您需要以下内容:

do{
    ....

    productValid = false;

    if (product == "hamburger")
    {   
    price = bprice; 

    discount = .1;
    productValid = true;

    }
    else if (product == "cheeseburger")
    {   
    price = chprice;

    discount = .05;
productValid = true;

    }

    else if (product == "soda")
    {   
    price = sprice;

    discount = .07;
productValid = true;
    }

    else if (product == "fries")
    {   
    price = fprice;

    discount = .15;
productValid = true;
    }   

    else{
        alert("Sorry, " +username+  " Your item not found.");
    }

if(productValid){
        cost=price*quantity
        discount=price*discount*quantity
        total=cost-discount
    document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>");
    document.write("This discount for this purchase is $" +discount+ ".<br/>");
}
else
{
    document.write("No valid product selected<br>");
}
 a = confirm("Do you want to place another order?");

    }while(a==true)

You're re-iterating the loop if the user hits cancel . 如果用户点击cancel那么您将重复循环。 Change while(a==false) to while(a==true) or simply while(a) while(a==false)更改为while(a==true)或简单地while(a)

Also put the line 还把线

a = confirm("Do you want to place another order?");

as the last line in your loop, instead of the line after. 循环的最后一行,而不是以后就行了。

You can set a flag var that lets you know if the item was found or not and check it at the end of your loop to avoid displaying data for non-existing items: 您可以设置一个标志var,让您知道是否找到了该项目,并在循环结束时对其进行检查,以避免显示不存在的项目的数据:

<script type="text/javascript">
    /*Daniel Weiner, Fengpeng Yuan, Javascript 2, Nov 4,2011*/

    var username;
    var bprice= 8;
    var chprice= 9;
    var sprice= 2;
    var fprice= 4;
    var price= 0;
    var a;
    var b;
    var product= "hamburger, cheeseburger, soda, fries";
    var quantity =0;
    var total;
    var cost = 0;
    var discount= 0;
    var flag;

    do{
    flag = true;  //assume found unless otherwise
    username =prompt("Welcome to Big Ben's Burgers. Please enter your name.", "");
    alert("Hello " + username+". Please look through our available products and services before placing your order.","");
    product=prompt("What do you want?","");
    quantity =1*prompt("How many of " +product+ " would you like?");

    if (product == "hamburger")
    {   
    price = bprice; 

    discount = .1;
    }
    else if (product == "cheeseburger")
    {   
    price = chprice;

    discount = .05;
    }

    else if (product == "soda")
    {   
    price = sprice;

    discount = .07;
    }

    else if (product == "fries")
    {   
    price = fprice;

    discount = .15;
    }   
    else{
        alert("Sorry, " +username+  " Your item not found.");
        flag = false;
    }

    if(flag){
        cost=price*quantity
        discount=price*discount*quantity
        total=cost-discount
        document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>");
        document.write("This discount for this purchase is $" +discount+ ".<br/>");
    }
    a = confirm("Do you want to place another order?");
    }while(a);
    alert('goodbye');
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM