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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Summing Help Needed in a 'for' loop

    Hi all,

    I am new to Javascript programming and this is also my first post on a forum so I hope I am using the right etiquette here!

    Please could somebody offer me some advice on how to sum the numbers from one to ?? as part of a 'for' loop? What I need for example is a program that adds the collective numbers 1+2+3+4+5 (etc) and outputs the total of 15. I have attached the file as I have written it so far.

    Thanks in advance for any help offered.
    Attached Files Attached Files

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another way to do this is to use the formula:

    (maxvar * (maxvar+1))/2

    This will only work if the lowest number = 1.

    If maxvar is 5 then:

    (5 * 6) / 2 = 15

    You can try with any number you want. It works.

    BaldEagle

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    /* testing a for loop1 */

    Code:
    var maxvar;
    var tempvar;
    var theSum = tempvar + maxvar  
    var maxvar = window.prompt('Enter value');
    for (tmpvar = 1; tmpvar <= maxvar; tmpvar++) {
    document.write('The number is ' + tmpvar + '<br>');
    }
    window.alert('The sum of the numbers from 1 to ' + maxvar + ' is ' + theSum);
    If your requirement is to use a loop then change it like this:

    Code:
    var maxvar;
    var tempvar;
    var theSum = 0;  
    var maxvar = window.prompt('Enter value');
    for (tmpvar = 1; tmpvar <= maxvar; tmpvar++) {
    theSum += tempvar;
    // you don't really need this as a large number would write
    // a lot of numbers to the screen
    document.write('The number is ' + tmpvar + '<br>');
    }
    window.alert('The sum of the numbers from 1 to ' + maxvar + ' is ' + theSum);
    BaldEagle

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi BaldEagle,

    Thank you for replying to this query, I really appreciate it. I do need to write it as a 'for' loop as you have shown in your replacement code for me, although I have run the program again with the new code and I am still getting the message 'the sum of 1 to 5 is NaN', is it something to do with the way sum has been valued?

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Lisbon, CT
    Posts
    339
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BaldEagle
    /* testing a for loop1 */

    Code:
    var maxvar;
    var tempvar;
    var theSum = tempvar + maxvar  
    var maxvar = window.prompt('Enter value');
    for (tmpvar = 1; tmpvar <= maxvar; tmpvar++) {
    document.write('The number is ' + tmpvar + '<br>');
    }
    window.alert('The sum of the numbers from 1 to ' + maxvar + ' is ' + theSum);
    If your requirement is to use a loop then change it like this:

    Code:
    var maxvar;
    var tempvar;
    var theSum = 0;  
    var maxvar = window.prompt('Enter value');
    for (tmpvar = 1; tmpvar <= maxvar; tmpvar++) {
    theSum += tempvar;
    // you don't really need this as a large number would write
    // a lot of numbers to the screen
    document.write('The number is ' + tmpvar + '<br>');
    }
    window.alert('The sum of the numbers from 1 to ' + maxvar + ' is ' + theSum);
    BaldEagle
    My mistake.

    Code:
    var maxvar;
    var theSum = 0;  
    var maxvar = window.prompt('Enter value');
    for (tmpvar = 1; tmpvar <= maxvar; tmpvar++) {
    theSum += tmpvar;
    // you don't really need this as a large number would write
    // a lot of numbers to the screen
    document.write('The number is ' + tmpvar + '<br>');
    }
    window.alert('The sum of the numbers from 1 to ' + maxvar + ' is ' + theSum);

    Try that.

    BaldEagle

  • #6
    New Coder
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi BaldEagle,

    Thank you very much indeed for your help with this today, the program as it is now written is exactly what I was after. I very much appreciate this.

    Thanks again



  •  

    Posting Permissions

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