2

I have an array [1,1,1,0] (any size of), the minimum length of array and number 1 is 3. I want to keep moving the value of 1s 1 element up so it could move to [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 reset 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,0,0,0,1]-> [1,1,1,0,0,0]-> [0,1,1,1,0,0]-> [0,0,1,1,1,0]->

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);
}
HelpNeeder
  • 5,933
  • 21
  • 80
  • 141

3 Answers3

2
  • First pop(store it).So [abcd]->[abc] and you have stored d.
  • Reverse. So [abc]->[cba].
  • Add the popped element. So [cba]->[cbad]
  • Reverse. So [cbad]->[dabc].

I hope you wanted that? See this too.


Upvote if it helped you. :(

Community
  • 1
  • 1
RE60K
  • 601
  • 1
  • 6
  • 26
  • Thanks to the existence of both `push()`/`pop()` and `unshift()`/`shift()`, you don't need to reverse the array. See my answer: http://stackoverflow.com/a/28329759/129655. – Platinum Azure Feb 04 '15 at 19:29
  • 1
    Fair enough. You had the right idea from algorithmic perspective; just take this as another learning opportunity. :-) – Platinum Azure Feb 04 '15 at 19:39
2

Depending on which direction you want to do, I would suggest using shift() and push(), or pop() and unshift().

Basically, shift() and unshift() add or remove an element from the front of the array, and push() and pop() add or remove an element from the back of the array.

For the example you have given, you'll probably want to pop() off the end of the array and unshift() to the front:

function rotateArray(arr) {
    var element = arr.pop();
    arr.unshift(element);
}
// Repeat in loop, timeout, etc. as necessary
Platinum Azure
  • 41,456
  • 9
  • 100
  • 130
  • Ah, exactly what I needed with an example. Thank you. – HelpNeeder Feb 04 '15 at 20:10
  • 2
    You could also add a `dir` parameter (direction) and implement as such: `function rotateArray(arr, dir) { dir = !dir || dir > 0; arr[dir ? "unshift" : "push"](arr[dir ? "pop" : "shift"]()); }` - see this [fiddle](http://jsfiddle.net/uxanykaw/) – Ryan Wheale Feb 04 '15 at 20:57
1
var arr = [1, 1, 1, 0, 0, 0],
    startIndex = arr.indexOf(1),
    sliceLength = 3,
    slice;

while(arr[arr.length - 1] === 0) {
    // slice out the 1's (modifies the original array)
    slice = arr.splice(startIndex, sliceLength);
    // splice the 1's back in, one spot over
    arr.splice.apply(arr, [++startIndex, 0].concat(slice));
    console.log(arr);
}

http://jsfiddle.net/sn7ujb90/1/

Ryan Wheale
  • 18,754
  • 5
  • 58
  • 83