2

I was wondering how to pass a number to a function I created for a loop. For example, I have a loop that simply adds 1 to a value when it runs. How would I go about passing how many times I want the loop to run in a function? Like so:

var i = 0;
function blahBlah (i ?){
for (i=0,i>10(this is what I want to pass to the function),i++){
i++;
}

Then call the function:

blahBlah(number of times I want it to run);
3
  • Can you provide more context for your question? I can't figure out what exactly you need to happen. Commented Jul 21, 2012 at 22:35
  • What would you do in ActionScript? Commented Jul 21, 2012 at 22:38
  • 1
    No worries, there are no noob questions, just strange answers. ;) Commented Jul 21, 2012 at 22:58

6 Answers 6

4

I'm not sure i understand the question, but how about

function blahBlah(n) {
    for(var i=0; i < n; i++) {
        //do something
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2
function blahBlah (noOfTimes){
  for (var i=0 ;i < noOfTimes ;i++){
    //i++; you already incremented i in for loop
    console.log(i);//alert(i);
    }
  }

blahBlah(10);// call function with a loop that will iterate 10 times

1 Comment

:) uh... 1/3 cup left... If I see a funny answer I'll go to take a new one ;)
2

You mean calling the function each iteration?

function blahBlah( i ) {
    // do something with i
}

for ( var i = 0; i < 10; i++ ) {
    blahBlah( i );
}

Comments

2

Maybe like this:

function runLoop(length) {
    for (var i=0; i < length; i++) {
        {loop actions}
    }
}

Comments

1

First, you used , instead of ; in for loop.

Second, you need two variables here: the first one is how many times to repeat (i, the argument), the second is a counter (a, which iteration is it now)

function blah(i) {
    for (var a=0; a<i; a++) {
        doStuff();
    }
}

Comments

1

Use a loop inside your function:

function BlahBlah(n) {
   for (i=0; i < n; ++i) {
      // do something...
   }
}

or simply invoke the function in a for loop:

function Blahblah() { /* do something */ }

// elsewhere:
n = 42;
for (i=0; i < n; ++i) BlahBlah();

1 Comment

in each version, you may use n itself as counter, like while (n--) BlahBlah();

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.