Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript do-while looping problems
    primarykey
    data
    text
    <p>I need help with looping the javascript in a do/while statement loop. </p> <p>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. I need help in order to make sure that the incorrect information won't be displayed. </p> <p>Also, when I hit "ok" to add more to my order, it doesn't loop it but merely displays the document.write. I want it to loop if you hit the "ok" button.</p> <p>Thank you for the help.</p> <p>Here is the code:</p> <pre><code> &lt;html&gt; &lt;head&gt;&lt;title&gt;Javascript Assignment 3: Daniel Weiner&lt;/title&gt; &lt;/head&gt; &lt;body bgcolor="brown"&gt; &lt;h1 align="center"&gt;Big Ben's Burgers-eCommerce&lt;/h1&gt; &lt;table border="1" width="100%" height="450px" bgcolor="gray"&gt;&lt;tr&gt; &lt;th align="center"&gt;Product/Service&lt;/th&gt; &lt;th align="center"&gt;Price&lt;/th&gt; &lt;th align="center"&gt;Discount&lt;/th&gt; &lt;/tr&gt; &lt;tr bgcolor="orange"&gt; &lt;td align="center"&gt;&lt;font size="3"&gt;Hamburger &lt;br&gt; &lt;br&gt; &lt;a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.256.Hamburger.html" target="_blank"&gt;Classic Hamburger&lt;/a&gt; &lt;td align="right" bgcolor="orange"&gt;&lt;font size="3"&gt;$8.00&lt;/font&gt;&lt;/td&gt; &lt;td align="center" bgcolor="orange"&gt;&lt;font size="3"&gt;.10&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr bgcolor="orange"&gt; &lt;td align="center"&gt;&lt;font size="3"&gt;Cheeseburger &lt;br&gt; &lt;br&gt; &lt;a href="http://www.mcdonalds.com/us/en/food/product_nutrition.sandwiches.284.cheeseburger.html" target="_blank"&gt;Classic Cheeseburger &lt;/a&gt; &lt;/font&gt;&lt;/td&gt; &lt;td align="right" bgcolor="orange"&gt;&lt;font size="3"&gt;$9.00&lt;/font&gt;&lt;/td&gt; &lt;td align="center" bgcolor="orange"&gt;&lt;font size="3"&gt;.05&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr bgcolor="orange"&gt; &lt;td align="center"&gt;&lt;font size="3"&gt;Soda &lt;br&gt; &lt;br&gt; &lt;a href="http://www.mcdonalds.com/us/en/food/full_menu/beverages.html" target="_blank"&gt;Fabulous Drinks&lt;/a&gt; &lt;/font&gt;&lt;/td&gt; &lt;td align="right" bgcolor="orange"&gt;&lt;font size="3"&gt;$2.00 &lt;/font&gt;&lt;/td&gt; &lt;td align="center" bgcolor="orange"&gt;&lt;font size="3"&gt; .07&lt;/font&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr bgcolor="red"&gt; &lt;td align="center"&gt;&lt;font size="3"&gt; French Fries &lt;br&gt; &lt;br&gt; &lt;a href="http://www.mcdonalds.com/us/en/food/product_nutrition.snackssides.120.small-french-fries.html" target="_blank"&gt; Fries&lt;/a&gt; &lt;/font&gt;&lt;/td&gt; &lt;td align="right" bgcolor="red"&gt;&lt;font size="3"&gt; $4.00&lt;/font&gt;&lt;/td&gt; &lt;td align="center" bgcolor="red"&gt;&lt;font size="3"&gt;.15&lt;/font&gt;&lt;/td&gt; &lt;/table&gt; &lt;script type="text/javascript"&gt; /*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+ ".&lt;br/&gt;"); document.write("This discount for this purchase is $" +discount+ ".&lt;br/&gt;"); }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+ ".&lt;br/&gt;"); document.write("The total order cost is $" +total+ "."); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload