Rotate array by the given steps - Node.js Array

Node.js examples for Array:Array Value

Description

Rotate array by the given steps

Demo Code

/* rotate array by the given steps */
Array.prototype.rotate = function(p) {
  for(var l = this.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
    for(i = l; i > p; x = this[--i], this[i] = this[i - p], this[i - p] = x);
};

Related Tutorials