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
#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }