# how can i make sequence of numbers repeat?

## Recommended Posts

I am total beginner so please dont be mean or make fun but i am having dificulty understanding this

i have this basic script I run in Stellatium that uses some javascripts functions but not all of them for scripting.

for (i = 0; i < 5; i++)
{
core.debug( + i)
}

it produces:

0
1
2
3
4

using only for loops or while loops how can I make it repeat over and over indefinatly ?
1
2
3
4

0
1
2
3
4

0
1
2
3
4

##### Share on other sites

Do you want it to loop forever?

```while(true){

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

}
```

...of course the browser might not be happy with this, because Javascript is intended to be event-driven and not a continuous user of processor time.

##### Share on other sites

Having it continue indefinitely would crash the browser, you should put a limit on how many times it displays a number.

The way to do it without nested loops would be like this:

```var amount = 20; // Show 20 numbers
var highestValue = 5; // Show numbers between 0 and 5

for(var num = 0, counter = 0; counter < amount; counter++, num++) {
if(num > highestValue) {
num = 0;
}
console.log(num);
}```
##### Share on other sites

thank you

Devoted and FoxyMod

Devoted..yes yours did crash but was still helpful

FoxyMod yours worked as expected

Foxy Mod I would be interested in the nested version to see how that is done as well

if you would like to share that with me that would be helpfull as well. Thank you.

##### Share on other sites

You should understand by now how loops work, you probably can figure it out on your own.

Here's a version with nested loops.

```var times = 4; // Display the sequence four times
var highestValue = 5; // Show numbers between 0 and 5

for(counter = 0; counter < times; counter++) {
for(var num = 0; num <= highestValue; num++) {
console.log(num);
}
}
```
##### Share on other sites

One approach that you could play with would be to not run such a loop continuously, but run it in a periodic manner...

```var si = setInterval(myLoop, 200);

function myLoop() {
for(var i=0 ; i<5 ; i++){
console.log(i);
}
}
```

...and there is also the newest approach...

http://www.w3schools.com/html/html5_webworkers.asp