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: For Loop Problems - Beginner.

1. ## For Loop Problems - Beginner.

Okay I'm having a problem getting this code to work. I've tried different things. The application is supposed to calculate the sum of two numbers the user enters and the numbers inbetween those numbers. For example 10 and 15 would make 75 (10+11+12+13+14+15) There's nothing in the parameters of the for loop as I have tried different things but I don't think I've come up with a possible solution.

Code:
```<html>

<script type="text/javascript">
function calculation() {
var a = document.getElementById("oneNum").value;
var b = document.getElementById("twoNum").value;
for (a=a;a=b;a++)
{

}
document.getElementById("sum").innerHTML = "The Sum is " + totalsum.toFixed(2);
}
</script>

<body>

<h2>Sum Application</h2>
<p>This calculator calculates the sum of the numbers between two numbers inclusive that are entered by the user. <br />
For example, if the user enters 10 and 15 the application would calculate 75 (10+11+12+13+14+15).<p>

Enter first number:
<input type="text" id="oneNum">
Enter second number:
<input type="text" id="twoNum">
<br />
<input type="button" value="Calculate" onClick="calculation()">
</body>

<p id="sum"></p>

</html>```

• Code:
```function calculation()
{
var a = parseInt(document.getElementById("oneNum").value);
var b = parseInt(document.getElementById("twoNum").value);
var total = 0;
for (var n=a; n <= b; n++)
{
total += n;
}
document.getElementById("sum").innerHTML = "The Sum is " + totalsum;
}
</script>```
(1) You *MUST* convert the *STRINGS* (all ".value" properties are strings!) to NUMBERS. Hence parseInt.

(2) You were using a=b as the condition for ending your loop.
The = alone is *ALWAYS* an assignment in JS. Not that any kind of equality test made sense. The middle expression in "for" is a "continue while TRUE" expression, not "continue until false".

(3) Always initialize variables.

• ## Users who have thanked Old Pedant for this post:

PivotalCandle (09-26-2009)

• Thank you very much incredibly. I see now that I must do more reading on the language before I attempt stuff a little bit too advanced. I understand the code completely now. The only thing that I see that I'm not sure is that for:

Code:
`total += n;`
The total varible becomes the sum of the equation?

• Oh, sorry. That kind of expression is used in most modern languages:
C, C++, C#, Java, JavaScript, PHP, and now even VB.NET

a += expression;

is simply shorthand for

a = a + expression;

Most of the operators can be used in this way:
+= -= *= /= ^= %= &=

It's just syntactic sugar (look that up on wikipedia...they have a great definition of the term). No reason to use it if it looks strange to you.

• By the by, I *could* have followe your lead and written:
Code:
```    for (a=a; a <= b; a++)
{
total += a;
}```
or could even write it like this:
Code:
```    for ( ; a <= b; a++)
{
total += a;
}```
But that really isn't any better code and I think it looks confusing. You could also write it as:
Code:
`    while ( a <= b ) total += a++;`
I'll let you cogitate on that one.

But, of course, the "right" way to write it is to be smart about the math:
Code:
```function calculation()
{
var a = parseInt(document.getElementById("oneNum").value);
var b = parseInt(document.getElementById("twoNum").value);
var total = (b+a) * (b-a+1) / 2;
document.getElementById("sum").innerHTML = "The Sum is " + total;
}
</script>```
But that gets you points in algebra, maybe not in programming. <grin/>

•

#### Posting Permissions

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