Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Incrementing with the While statement

1. 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>
<title>Investment</title>
<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,

• 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.

• 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

• 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Investment</title>
<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>```

• 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
•