View Full Version : Summing Help Needed in a 'for' loop

sulli1

01-23-2006, 04:17 PM

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.

BaldEagle

01-23-2006, 05:09 PM

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

BaldEagle

01-23-2006, 05:15 PM

/* testing a for loop1 */

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:

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

sulli1

01-23-2006, 05:32 PM

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?

BaldEagle

01-23-2006, 05:48 PM

/* testing a for loop1 */

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:

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.

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

sulli1

01-23-2006, 06:34 PM

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

:thumbsup:

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.