Skip to content

Instantly share code, notes, and snippets.

@nachodd
Created October 4, 2013 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nachodd/6830680 to your computer and use it in GitHub Desktop.
Save nachodd/6830680 to your computer and use it in GitHub Desktop.
Function that shifts array positions. (see example) Funcion que rota las posiciones de un array. (ver ejemplo) FUENTE: http://stackoverflow.com/questions/1985260/javascript-array-rotate CREDITOS: http://stackoverflow.com/users/48015/christoph
/*
FUENTE: http://stackoverflow.com/questions/1985260/javascript-array-rotate
CREDITOS: http://stackoverflow.com/users/48015/christoph
*/
Array.prototype.rotate = (function() {
var unshift = Array.prototype.unshift,
splice = Array.prototype.splice;
return function(count) {
var len = this.length >>> 0,
count = count >> 0;
unshift.apply(this, splice.call(this, count % len, len));
return this;
};
})();
/*
Ejemplo:
var bits = [1,1,1, 0,0,0, 0,0,0];
bits.rotate(-1); // [0,1,1, 1,0,0, 0,0,0]
bits.rotate(2); // [1,1,0, 0,0,0, 0,0,1]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment