Help to Modify the Eval Script Code

Can anyone modify this to get the array index from start to finish, and then back to start again? (ex: 0, 1, 2, 0, 1, 2, 0, 1, etc)

Spoiler: code
<After Eval>
var old = #
// Change # to the skill's required state
var newstate = $dataEnemies[user._enemyId].meta.states.split(',').map(function(n){return parseInt(n)});
// Reads the allowed state values from the enemy notetag
var index = Math.randomInt(newstate.length);
var choice = newstate[index]
if (user.isStateAffected(choice)) {
} else {
BattleManager._subject.removeState(old);
BattleManager._subject.addState(choice);
}
</After Eval>

Original script:

I already tried to write it out myself, but my intelligence is not enough as of now. -_-
Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/help-to-modify-the-eval-script-code.125004/