...

View Full Version : For Loop Problems - Beginner.



PivotalCandle
09-26-2009, 05:35 AM
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.



<html>

<head>
<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>
</head>

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

Old Pedant
09-26-2009, 05:55 AM
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.

PivotalCandle
09-26-2009, 06:18 AM
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:


total += n;

The total varible becomes the sum of the equation?

Old Pedant
09-26-2009, 06:55 AM
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.

Old Pedant
09-26-2009, 07:11 AM
By the by, I *could* have followe your lead and written:


for (a=a; a <= b; a++)
{
total += a;
}
or could even write it like this:


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:


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:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum