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 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Infinite loop issue

    Hello,

    I was trying to make a simple prompt that would allow the user to append a string that is held in a variable and i wanted to give the user only 3 opportunities to do this. I tried this with a function and again for with a while, for some reason my loop is running beyond the parameter I set it for. There is no end game with this or ultimate goal, I am just trying to review somethings I learned and I am encountering issues.

    Code:
    var i = 0;
    
    while ( i <= 2 )
      var statement = prompt ("What would you like to add to this statement, you will have 3; opportunity", " ") ;
    i++;
    
    show (statement)

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,081
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Your statement
    Code:
    i++;
    is outside of the while loop so it is never executed.
    Should Be:
    Code:
    while ( i <= 2 ){
    	var statement = prompt ("What would you like to add to this statement, you will have 3; opportunity", " ") ;
    	i++;
    }
    But I'd use a for loop instead because of the infinite loop problem. Not better or worst, just different.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    codenogood (01-19-2014)

  • #3
    New Coder
    Join Date
    Dec 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Many thanks. I tried putting it in the brackets at one point but I also received an error because i defined the variable in the while, which I thought was okay.

    Code:
    while (var i <= 2;)
    Appreciate you taking the time to look at it.

  • #4
    New Coder
    Join Date
    Dec 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts
    As a follow up question does anyone know how to store multiple entries from a looping prompt like in my example?

    I am not looking for code per-say, just a point in the right direction so I can research the method of storing data from a looping prompt.

    Code:
    var i = 0;
    var state = "I state that it is better to have loved and lost ....."
    
    while ( i <= 2 ){
      var statement = prompt ("What would you like to add to this statement, you will have 3; opportunity", " ") ;
    i++;
      
    }
     show (state + statement)

    Currently it takes the last prompt that the user issues, was wondering how I could store and concatenate all of the three prompt responses.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type = "text/javascript">
    var i = 0;
    var state = "I state that it is better to have loved and lost ....."
    while ( i < 3 ) {
    var statement = prompt ("What would you like to add to this statement, you will have " + (3-i) + " opportunities", "") ;
    if (statement.length > 0) { // trap blank entries
    state += "\n"  + statement;
    i++;
    }
    }
    alert (state);
    </script>
    \n is the Javascript newline. Use <br> if your result is to be written out as HTML.


    A wise man never plays leapfrog with a unicorn - Tibetan proverb

    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
    New Coder
    Join Date
    Dec 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you, what is the

    3-i for? couldn't you just use 3?

    Code:
    var statement = prompt ("What would you like to add to this statement, you will have " + (3-i) + " opportunities", "") ;

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    For every prompt, the number of opportunities decreases.


  •  

    Posting Permissions

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