mercredi 4 février 2015

Move 3 elements of array by one in each of loop iterations


I have an array [1,1,1,0] but could be any size of an array, the minimum length of array and number 1's is 3, and I want to keep moving the value of 1's 1 element up so it could moveto [0,1,1,1], then back to [1,1,1,0].


My loop sort of work in recursive mode, but I'm not sure how to re-set the values, so after 4 loops, my entire array is filled with 0's.


More lengthily array would traverse as so:


Example:



[1,1,1,0,0,0]-> [0,1,1,1,0,0]-> [0,0,1,1,1,0]-> [0,0,0,1,1,1]->
[1,1,1,0,0,0]-> [0,1,1,1,0,0]-> [0,0,1,1,1,0]-> [0,0,0,1,1,1]-> and so on for ever...


Code:



function flip(images, active) {
var new_active = active;

setTimeout(function() {
active.reverse();
active[active.length] = 0;
active.reverse();
active.pop();

for(var i = 0; i < active.length; i++) {
alert(active[i]);
}

flip(images, active);
}, 5000);
}




Aucun commentaire:

Enregistrer un commentaire