0

Maybe I should use some kind of loop or something. But I don't have any idea how to declare this, excluding do it manually.

Is there any solution that will have only a few lines for all of this. Because, there should be 100 variable, despite I only presented 5, I didn't want to type all night.

a1=1; a2=1; a3=1; a4=1; a5=1;

function myfn1() {
a1++;
//unique function code
}
function myfn2() {
a2++;
//unique function code
}
function myfn3() {
a3++;
//unique function code
}
function myfn4() {
a4++;
//unique function code
}
function myfn5() {
a5++;
//unique function code
}
4
  • Any reason you are not using an array? Commented Mar 18, 2014 at 1:37
  • The reason is I don't know how to declare a fn with an array :( Commented Mar 18, 2014 at 1:39
  • I recommend to read a tutorial about arrays. Commented Mar 18, 2014 at 1:52
  • So you have 100 unique functions? Is it safe to assume that they're actually the same, except for the variable they use? If so, you can eliminate all that repetition. Commented Mar 18, 2014 at 1:55

1 Answer 1

1

Although this isn't answering your question, I think this approach will help you more in the long run (and probably in the short run too). Check out using an array, which is just a collection of data. Check out the following code.

var arr = [];
for(var i =0; i <= 100; i++){
  arr[i] = i;
}

this gives you a collection like this [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] that you can then manipulate, etc etc.

*edit or if you want them all to have the same value, then

var arr = [];
for(var i =0; i <= 100; i++){
  arr[i] = 1;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.