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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2005
    Location
    Alabama
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Incrementing with the While statement

    I am trying to build a while statement in school using a loop. In a while statement it keeps running until the statment is false. I understand you use a counter to increment the varaible so that it will keep running until the statement becomes false and stops. My question is there a method for incrementing and decrementing besides ++ and --. what I mean is can you use a calculation to increment like instead of ++counter or --counter like .15*counter . If so what format do you do this in. I would like to make a varaible increment by .15 everytime.
    I will list the code below:
    Code:
    <html>
    <head>
    <title>Investment</title>
    </head>
    <body>
    <script type="text/javascript">
    var goal = 2
    while ( goal < 20) {
    document.write(goal+"<br />");
    ++goal;  <<<<<<<<< Can I use a calculation here like  
                 <<<<<<<<<<<<<<<<<< (goal * .15) <<< if so what format>?
    }
    </script>
    </body>
    </html>
    Thanks,
    Mandriva Club = $12.00/month
    Internet connection = $29.00/month
    A decent laptop = $1200.00
    Switching to Linux = Priceless

    Linux Member # ?????

  • #2
    New Coder
    Join Date
    Jul 2005
    Location
    Alabama
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay I figured one thing out I can use the increment like this:
    Code:
    goal *=2
    but how can I use a floating number, I want to increment up by a percentage:
    Code:
    goal *=.15
    Can I use .15 or 15% do I have to declare floating numbers for some reason if I use .15 it turns into an infinite and locks up.
    Mandriva Club = $12.00/month
    Internet connection = $29.00/month
    A decent laptop = $1200.00
    Switching to Linux = Priceless

    Linux Member # ?????

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    goal *=.15

    This will decrement. Try this

    goal *=1.15 this will increase by 15% but it will not be linear. The increment amount will increase as goal increases. If you need the increment to be constant then capture the value first and then just add it as you go.

    BaldEagle

  • #4
    New Coder
    Join Date
    Jul 2005
    Location
    Alabama
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    BaldEagle,
    You are so write I actually figured that out before I came here. I was actaully coming back to post that I realized I was an idiot, hehe. It is funny I was trying to use an operand to decrement a value of the variable and cause it to run in an infinte loop. Thanks for your input because that was it I wish I would have talked to you earlier because it would have saved me a headache. Well I am still happy I found it on my own first and think that I am getting on the right track in my JavaScript class in college. Once you catch on it is like sailing. I had a big problem in the beggining just understanding return values in functions. Well thanks again and I will post the final code below, maybe it will help someone in the future!
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>Investment</title>
    </head>
    <body>
    <p>This represents every year in consecutive order to reach 1 million dollars!</p>
    <script type="text/javascript">
    <!-- Hide from incompatible browser
    var Investment = 10000;
    while (Investment <= 1000000) {
    	document.write("<p>Year " + Investment + "</p>");
    	Investment*=1.5;
    }
    // Stop hiding from browsers -->
    </script>
    </body>
    </html>
    Mandriva Club = $12.00/month
    Internet connection = $29.00/month
    A decent laptop = $1200.00
    Switching to Linux = Priceless

    Linux Member # ?????

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I do it all the time. Thanks for responding and posting the fix. Some do and some don't. Always wonder if those that don't post after getting suggestions/fixes are actually getting their problems fixed, or are just giving up.

    Baldeagle


  •  

    Posting Permissions

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