javascript array rotate [ 789 views ]
Goal: to rotate a javascript array
This is similar to the assembly shift operation with bites 01011 -> rotate left 2 -> 01101
var array = [0,1,0,1,1]; // 0,1,1,0,1
The solution is simple:
for (var i = 0; i < 2; i++) {
array.push(array.shift());
}
[/js]
- The shift() will cut/remove the first element from the array.
- The second step is to put/paste this element to the end of the array (push).
- Repeat the steps with a cycle