I am running a loop, and I am trying to create a variable each time the loop runs with the number of the counter appended to the end of the variable name.
Here's my code:
var counter = 1;
while(counter < 4) {
var name+counter = 5;
counter++;
}
So after the loop runs there should be 3 variables named name1, name2, and name3. How can I append the counter number to the end of the variable I am creating in the loop?
You're looking for an array:
var names = Array();
// ...
names[counter] = 5;
Then you will get three variables called names[0], names[1] and names[2]. Note that it is traditional to start with 0 not 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With