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: Summing Help Needed in a 'for' loop

1. ## 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.

• 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

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

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

• 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

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