0

I am not sure how to phrase this, so please re-title this question if it doesn't make sense. Anyways, this is what I am trying to do.

I have a variable with the length of 9. And then I have another variable with the length of 3.

How do I write a loop that iterates through all 9 but starts over every third time?

For example: I have this,

x = 3;
l = 9;

for ( var i = 0; i < l; i++)
{ 
     console.log(i + 1);
}

output = 1,2,3,4,5,6,7,8,9

The output I want to create

output = 1,2,3,1,2,3,1,2,3

I was thinking there might be away to do this with an if statement or possibly modulus, but wasn't quite sure how to implement it. What would be a good way to do this? Thanks in advance.

3
  • 1
    Use two for(){…} loops, one nested in the other? Commented Mar 28, 2014 at 19:09
  • 1
    console.log(i % x + 1) Commented Mar 28, 2014 at 19:11
  • @LightStyle, Yeah that is what I was looking for... I must have been doing it wrong when I tried it. Commented Mar 28, 2014 at 19:19

6 Answers 6

2

Embrace the modulus:

function expand(length, loop_length) {
  for (var i = 0; i < length; i++) {
    console.log(i % loop_length + 1);
  } 
}
expand(9, 3) // =>  1, 2, 3, 1, 2, 3, 1, 2, 3
Sign up to request clarification or add additional context in comments.

Comments

1
x = 3;
l = 9;

for ( var i = 0; i < l; i++)
{ 
     console.log(i % x + 1);
}

output = 1,2,3,1,2,3,1,2,3

See it in action here: http://jsfiddle.net/BgBGL/

12 Comments

I thought you could do it with modulus, I must have been doing it wrong. This is what I was looking for. Thanks!
No problem, I missed it at first as well.
I'm crying, code snipped with syntax errors won. :(
Don't cry, what syntax errors? Or are you referring to that you finally saw what works and used for your answer?
Just FYI, I am using var x = somevar; or whatever in my actual code that was something I had just typed up quick to illustrate my problem.
|
0

If you want to loop from a min value to a max value a specific number of times, the easiest way is just to have 2 loops, like this:

var min = 1, max = 3, times = 3;
for (var i = 0; i < times; i++) {
    for (var j = min; j <= max; j++) {
         console.log(j);
    }
}

Or if you want to fix total length of the sequence, then yes, you can do it with a single loop and a little bit of math:

var min = 1, max = 3, totalLength = 9;
for (var i = 0; i < totalLength; i++) {
    console.log((i % (max - min + 1)) + min);
}

Comments

0

For that case, this works:

x = 3;
l = 9;

for ( var i = 0; i < l; i++)
{ 
     var num=(i %(x+1)) || 1;
     console.log(num);
}

Comments

0

You could go mad with following syntax:

var i = 0;

do {
  console.log(i++ % 3 + 1);
} while (i < 9);

Alternative, you could define 3 and 9 as a variable also.

I took an advantage of the fact that calling i++ will display old variable and increases it by 1 after, so I saved some bits!

See: fiddle example

Comments

0
x = 3;
l = 9;

for ( var i = 0; i <= l; i++)
{ 
  for ( var j = 1; j <= x; j++)
   { 
     console.log(j);
   }
}

4 Comments

I suggest you test this. How are j + x going to give 1 when x is 3, and j is >= 0?
Better, there are two more things - the outer loop will run 10 times instead of 9 and the inner one 3 times each, making the total number of printed numbers 30 instead of 9.
I tested it and it gives 123 nine times. but you're right 30 loop is far answer from the main question.
Yes, sorry, my bad - it's 27 instead of 9. :) The easiest fix is probably: for ( var i = 0; i < l/x; i++) for the outer loop.

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.