5

How to simplify a set of vars?

        var q1 = 0;
        var q2 = 0;
        var q3 = 0;
        var q4 = 0;
        var q5 = 0;
        var q6 = 0;
        var q7 = 0;
        var q8 = 0;
        var q9 = 0;
        var q10 = 0;
        var counter = 0;

or like this?

var q1 = 0, q2 = 0, q3 = 0, q4 = 0, q5 = 0, q6 = 0, q7 = 0, q8 = 0, q9 = 0, q10 = 0, counter = 0;

Is there any way around?

Raissa Ricarde
  • 108
  • 1
  • 11
  • 2
    Perhaps an array? – jackarms Aug 15 '17 at 05:02
  • please see: https://stackoverflow.com/questions/5944749/how-do-i-declare-and-use-dynamic-variables-in-javascript – huan feng Aug 15 '17 at 05:02
  • I am not clear on what your question is asking. – wmorrell Aug 15 '17 at 05:02
  • 2
    Or you could use an Array ... `var q=[0,0,0,0,0,0,0,0,0,0]` – Carsten Massmann Aug 15 '17 at 05:03
  • Just use `var q = 0;` and a `counter = 0;` – user8256287 Aug 15 '17 at 05:04
  • @Shree When you see ten variables named `q1` through `q10`, doesn't this suggest that the actual question is not how to define multiple variables on a line, but instead how to deal with ten very similar things? Is there another way to do this in JavaScript? – Michael Geary Aug 15 '17 at 07:23
  • 1
    @Raissa The real question here is what do you want to do with these ten variables. Are they ten of a kind? That is, are `q1` through `q10` all instances of essentially the same thing? If so, you should definitely use an array. Here's a simple example: suppose you have a carton of 12 eggs, and you need to write some code that deals with each egg in some way. Would it make sense to have twelve individual unrelated variables `egg1` through `egg12`? Or would it be better to be able to deal with each egg just like the others? It's probably the latter, so an array is what you're looking for. – Michael Geary Aug 15 '17 at 07:31
  • Yes @MichaelGeary you're right! thanks – Raissa Ricarde Aug 15 '17 at 07:44
  • Gosh, @Shree and @Community, the stated duplicate is not a duplicate of this question _in any way_. That question is about how to declare multiple _unrelated_ variables on the same line, just as it says. This question is entirely different. It's "I have ten very similar items I need to deal with. Is there a better way to do this than use ten variables with names like `q1` through `q10`?" And of course the obvious answer to an experienced JavaScript programmer is "use an array". None of the answers to that other question even mention arrays. Not the same question at all! – Michael Geary Aug 15 '17 at 08:30
  • Of course I'm not saying there may not be another duplicate question out there somewhere. The question of how to handle a number of similar items in JavaScript (or other languages) has come up again and again. If there is a better duplicate, I'm all for linking to that. I'm just saying that the "duplicate" listed above is not a duplicate at all - it's a completely different question. – Michael Geary Aug 15 '17 at 08:37

4 Answers4

1

If you use series variables. The best option is the array

 var q=[0,0,0,0,0,0,0,0,0,0] , counter=0;
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
0

Please check answers in this post:

How do i declare and use dynamic variables in javascript?

for(var i=1;i<=10;i++) {window['q' + i] = 0};
var counter =0;
huan feng
  • 7,307
  • 2
  • 32
  • 56
0

Try the following code:

for (var q = 1; q<10; q++) 
{ 
// do what you want
//ie.
// counter = counter + q;
}
Fabien
  • 4,862
  • 2
  • 19
  • 33
user8256287
  • 177
  • 15
-1

Probably simplest is

var q1 = q2 = q3 = q4 = q5 = q6 = q7 = q8 = q9 = q10 = counter = 0;

But wait

Having those many variables might land you to further problems.

Better you have an array with all these values. Cause to refer them you have to name them each time =. But with arrays you can have a opportunity to loop them and access them with index.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307