Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JavaScript Looping Help

    Hi I was wondering if someone could help me with my coding so it could loop.

    <script type="text/javascript">

    var username;
    var hprice= 129;
    var tprice= 172;
    var sprice= 17;

    var bprice= 4;

    var price= 0;

    var a;

    var b=true;
    var product= "hotel, tickets, shirt, sticker";

    var quantity =0;
    var total=0;
    var cost = 0;
    var discount= 0;
    var totalcost=0;
    var runningtotal= 0;
    do{

    username =prompt("Welcome to Navigate New Jersey. 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 == "hotel")
    {
    price = hprice;

    discount = .1;
    }
    else if (product == "tickets")
    {
    price = tprice;

    discount = .05;
    }

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

    discount = .07;
    }

    else if (product == "sticker")
    {
    price = bprice;

    discount = .15;
    }

    else{
    c=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/>");

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

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



















    </script>

    I need it to loop so a hypothetical customer can order more products from my made up website.

    Thanks.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Just put a
    Code:
    while ( true ) 
    {
    at the top of the section you want to loop and then put this at the end of the loop:
    Code:
        if ( ! confirm("Do you want to place another order?") ) { break; /* out of the loop */ }
    }
    No need for the silly a and b variables used just as flags.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • The Following 2 Users Say Thank You to Old Pedant For This Useful Post:

    saqisgd (03-29-2013), weiner992 (03-29-2013)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    That will not work! You are using document.write().

    document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    Prompts and alerts are also obsolete. You should use DOM methods to obtain input data from the user and display messages.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ??? Where do you see that he is using document.write after the page loads, Philip?

    Wasn't it you, just a day or so ago, who got all over me for insisting on getting rid of document.write?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Old Pedant View Post
    ??? Where do you see that he is using document.write after the page loads, Philip?

    Wasn't it you, just a day or so ago, who got all over me for insisting on getting rid of document.write?
    document.write("The cost of buying " +quantity+ " of " +product+ " is $" +cost+ ".<br/>");
    document.write("This discount for this purchase is $" +discount+ ".<br/>");

    If he loops through repeatedly by requesting a new product then the page will be erased.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No, not true, Philip.

    His loop is all withing the same block of JavaScript code and is dependent only on prompts and confirms.

    Hey, if what you say is true, then *NONE* of these hundreds of homework problems involving loops that we see each year would ever work with just prompt and document.write.

    But here, try this little page to convince yourself, perhaps:
    Code:
    <html>
    <body>
    <h3>Handy dandy payroll calculator</h3><br/>
    
    <script type="text/javascript">
    var cnt = Number( prompt( "How many names do you want to enter?" ) );
    var total = 0;
    while ( cnt-- > 0 )
    {
        var name = prompt("Enter a name");
        var salary = 20000 + Math.random() * 40000;
        total += salary;
        document.writeln( name + " is paid $" + salary.toFixed(2) + "<br/>");
    }
    </script>
    <hr/>
    Total payroll: 
    $<script type="text/javascript">document.write(total.toFixed(2))</script>
    <hr/>
    <h3>Come again soon!</h3>
    </body>
    </html>
    Notice that I'm even allowed to mix HTML and JS coding.

    Remember: All this is happening *before* the page is fully loaded. That is, before the </body> tag is encountered.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by Old Pedant View Post
    No, not true, Philip.
    I agree and using a while loop is the way to go. But since this is clearly a homework exercise I don't like the use of break; unless it is absolutely necessary like in a switch block. Using break unnecessarily teaches bad habits imo and as akin to the bad old days of spaghetti code.

    A better way imo is to use proper logic to exit loops. For example.

    Code:
    while(placeAnotherOrder){
         // do stuff here, then
         placeAnotherOrder = confirm("Place another order?");
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You and I will have to agree to disagree about break being bad coding. I can't see any way in which breaking out of a loop early is bad coding.

    Anyway, if you are going to insist on not doing so, then your code is, in my opinion, bad. As it requires initialization of the loop flag:
    Code:
    var placeAnotherOrder = true;
    while(placeAnotherOrder){
         // do stuff here, then
         placeAnotherOrder = confirm("Place another order?");
    }
    If you want a purer form, then use:
    Code:
    do {
       ...
    } while ( confirm("Place another order?") );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    yes, do-while is even better

  • #10
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    assuming at least 1 prompt is required, otherwise just a WHILE loop.

    [ot]
    How come I have no Edit button on my posts. Other sites I post on all have an edit button for a set period of time before it disappears.

    Is it just my account??

    I guess this is one way of boosting post count

    [/ot]

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    See the "Announcement: New Usergroup and Promotion Strategy Changes!" just above the first thread in any forum.

    That's a recent change, because the spammers were getting so agressive.

    But you can probably ask for dispensation. It's obvious that you have good stuff to contribute and for sure aren't a spammer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post
    You and I will have to agree to disagree about break being bad coding. I can't see any way in which breaking out of a loop early is bad coding.
    Because it causes a deviation from the expected flow of execution, making debugging by others unnecessarily arduous.
    Premature return statements cause exactly the same problem.
    Some are born with that realisation, some can be taught, and then there are the others.
    Last edited by Logic Ali; 03-30-2013 at 05:03 AM.


  •  

    Tags for this Thread

    Posting Permissions

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