# Multiple variables in for statement

• Aug 4, 2003, 14:31
ASchweti
Multiple variables in for statement
Hi there,

I've written some code to calculate a price based on some options choosed by the client.

Now I would like to multiply this code by using a for statement and I am wondering if there is a way to have different variables for each run.

What I mean:

I have e.g.

for (i=1; i <= 3; i++) {
...
ammount1 = 0;
...
}

If i=1 the variable should be ammount1, if i=2 it should be ammount2 and so on. Is there a way to do this? ammount+i = 0 obviously doesn't work :(

Thanks and regards,

ASchweti
• Aug 4, 2003, 14:47
beetle
You could use an array
Code:

```var amounts = new Array(); for ( var i = 0; i < 3; i++ ) {         amounts[i] = 0; }```
Will that work?
• Aug 4, 2003, 14:56
ASchweti
I am trying to solve this at this very moment, so I was playing around with arrays when I received your reply.

So to me it sounds like the logical solution, but I am getting a "amount is not defined" :(

Any idea why?
• Aug 4, 2003, 15:02
ASchweti
I believe I found it?

My smart O'Reilly book told me to add
Code:

`amount = new Object();`
It seems to work. Is that the correct way to go?

Thanks ASchweti
• Aug 4, 2003, 15:07
beetle
I'd have to see more of your code to understand the error.
• Aug 4, 2003, 15:17
ASchweti
Hi Beetle,

just read my second post right after yours, that I solved this issue. I just was not sure if I solved it the correct way?!

Anyhow I am facing another problem!

In order to calculate different amounts based on user entries I am using PHP to create the appropriate amount of forms.

The form fields are named e.g. color1 on the first form, color2 on the second form. In order to calculate the price I would need to get a hold of the values.

So again if i=1 I need to check
Code:

`document.form.color1.checked`
if i=2 I need to check
Code:

`document.form.color2.checked`
an so on...

Do you know what I mean? Is there a way to keep this flexible as well?
• Aug 4, 2003, 16:24
beetle
yes

document.form.elements['color'+i].checked
• Aug 5, 2003, 11:43
ASchweti
Hi Beetle,

works great! Thanks very much.

Best regards,

Alex