www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with a simple loop

  1. #1
    Join Date
    Oct 2012
    Posts
    5

    Help with a simple loop

    I'm supposed to make a simple order calculator using a loop. I need to have a web page which asks a user what they want to order, when they type in the name of the product and the quantity, the web page will look up and calculate the price. The page then asks them if they want to order more and if they click okay, another box comes up again asking them what they want to order and how much they want. The whole time, a running total is supposed to be kept so that when they finally click that they do not want to order anymore, the program terminates and shows them their total.

    This is the code I have so far:

    PHP Code:
    var nameproductquantityconfirmtotalpricediscount;
        
        var 
    name prompt("Welcome to Ice Cream Shop. What is your name?""Enter name");
        var 
    sentence "Hello " name " please look through our available products and services before placing your order.";
        
    alert(sentence);
         
        var 
    total 0
        var 
    price 0;
        var 
    discount 0;
        
        do 
        {
        var 
    product prompt("What would you like to order?","");
        var 
    quantity prompt ("What would you like to order?","");
        
    total total price;
        var 
    confirm = continue("Do you want to order anything else?");
        }
        
        while (
    confirm==true);
        
        if(
    product === "ice cream cake") {
                        
    price 20 discount;
                        
    discount = (.15 price);
                } else if (
    product === "ice cream cone") {
                        
    price discount;
                        
    discount = (.01 price);
                } else if (
    product === "small ice cream sundae") {
                        
    price discount;
                        
    discount = (.05 price);
                } else if (
    product === "large ice cream sundae") {
                        
    price discount;
                        
    discount = (.05 price);
                } else 
    alert("Sorry, " name ". You entered an invalid product. Refresh the page to reload and place the order again.");
                }
        else 
        {
                
    alert("Refresh the page to reload and place a new order");
        }
            
        if (
    product == "ice cream cake" || product == "ice cream cone" || product == "small ice cream sundae" || product == "large ice cream sundae") {
        
        
    document.write("Thank you for placing an order with us, " name ".");
        
    document.write("</br>");
        
    document.write("With the discount, your total order cost is " total ".");
        
        }
        else
        {
        
        
    document.write("Refresh the page to reload and place a new order");
        
        } 
    Any help would be appreciated because I am unbelievably confused.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,414
    If you try to use document.write() in your loop, you are destined for failure.

    The document.write() can only be used while the original page is loading. Anytime after it is rendered, the document.write() will reset and reload the page to the original contents. This loses all changes the user had made and essentially starts over.

    You should investigate uses a <div> or <span> element and write contents to them using an .innerHTML attribute.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles