Results 1 to 2 of 2

Thread: Getting a loop to repeat by resetting variable

  1. #1
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default Getting a loop to repeat by resetting variable

    this code will only execute twice.
    what I want it to do is reset the value and start the loop over again
    I don't know why it only works twice.


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/stict.dtd">
    <html>
    <head>
    
    <title>do while experiment</title>
    
    </head>
    <body>
    you have 6 shots
    
    
    <script type="text/javascript">
    
    var load = 0;
    bang();
    function bang()
    {
    
    do {alert("you have " + load +" shots fired allready");
    
    	load++;
    
    	}
    
    	while (load <7);
    }
    
    if
    (load = 7)
    {
    
    alert("you are out of shots-click to reload");
    load = 0;
    bang();
    
    }
    
    
    </script>
    
    
    </body>
    Last edited by Snookerman; 04-23-2009 at 07:40 PM. Reason: changed [quote] tags to [code] tags

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think your function ends before you intend it to (some blank lines removed for clarity, end comment added):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/stict.dtd">
    <html>
    <head>
    <title>do while experiment</title>
    </head>
    <body>
    you have 6 shots
    <script type="text/javascript">
    var load = 0;
    bang();
    function bang()
    {
    do {alert("you have " + load +" shots fired allready");
    load++;
    }
    while (load <7);
    } //bang() ENDS Here
    if
    (load = 7)
    {
    alert("you are out of shots-click to reload");
    load = 0;
    bang();
    }
    </script>
    </body>
    If you want it to be continuous:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/stict.dtd">
    <html>
    <head>
    <title>do while experiment</title>
    </head>
    <body>
    you have 6 shots
    <script type="text/javascript">
    var load = 0;
    bang();
    function bang()
    {
    do {alert("you have " + load +" shots fired allready");
    load++;
    }
    while (load <7);
    alert("you are out of shots-click to reload");
    load = 0;
    bang();
    }
    </script>
    </body>
    But I wouldn't recommend it, it will be an endless loop.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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