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 to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    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)

  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?
    Last edited by PivotalCandle; 09-26-2009 at 06:21 AM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    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
    •