DJ012 - 1 year ago 47

Javascript Question

I'm trying to write a little snippet where prompts ask the user for 5 numbers and computes the total of the numbers.

So far I have this:

`var counter, number, total;`

for(counter = 0; counter < 5; counter++) {

number = parseFloat(prompt("Enter a number:"));

total += number;

}

document.write("The total is " + total + ".");

However the 'total' returns " " for example, rather than a sum of 15.

How do I fix this?

Thanks in advance!

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You should initialize your `total`

var to 0:

```
var counter, number, total = 0;
for(counter = 0; counter < 5; counter++) {
number = parseFloat(prompt("Enter a number:"));
total += number;
}
document.write("The total is " + total + ".");
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**