Question: I have an array of numbers from 1 to 6.
But my for loop is greater than this number.
I want to make my array turn back to 1 when loop's max number is equal.
So basically if max number is 10 it should count as 1,2,3,4,5,6,1,2,3,4.

```
var myFiles =  new Array(1,2,3,4,5,6);
var filecount = myFiles.length;
var layercount = 10;
var f = 0;
var n = 0;

for(var m = 0; m < layercount; m++,n++) { 
    if (m > f) { 
        f = 0; 
        n = n - m; 
        f = f + n;
    }
    $.write (myFiles[f]);    
}

```

Answer: 
You could use the [remainder operator `%`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()) with the length of the array for the index of the array.

```js
var array = [1, 2, 3, 4, 5, 6],
    i,
    l = 10;

for (i = 0; i < l; i++) {
    console.log(array[i % array.length]);
}
```
Use `%`

```js
var myFiles =  new Array(1,2,3,4,5,6);
var filecount = myFiles.length;
var layercount = 10;
for( var m = 0; m < layercount; m++){ 
    console.log(myFiles[m % filecount])    
}
```