Nodejs Array Rotate rotate(K)

Here you can find the source of rotate(K)

Method Source Code

/*//from   ww  w. ja va2s .c o m
 * Rotate Array
 * Rotate an array of N elements to the right by K steps,
 * For example, with N = 7 and K = 3 the array [1,2,3,4,5,6,7]
 * is rotated to [5,6,7,1,2,3,4]
 *
 * Use example: [1,2,3,4,5,6,7].rotate(3) returns [5,6,7,1,2,3,4]
 * Also we can use [1,2,3].reverse(...)
 *
 * */

Array.prototype.rotate = function (K){
  var N = this.length;

  if( N  < 1 || K < 1 || K > N ){
    return this;
  }
  
  this.reverse(0, N - K - 1);
  this.reverse(N-K, N-1);
  this.reverse();

  return this;

};
Array.prototype.reverse = function(left, right){
  left  = (left || left === 0) ? left : 0;
  right = (right || right === 0) ? right : this.length-1;

  if(left < 0 || right >= this.length){
    console.log("Error!");
    return this;
  }

  while( left < right ){
    var tmpValue = this[left];
    this[left] = this[right];
    this[right] = tmpValue;
    left++;
    right--;
  }

  return this;
};

Related

  1. rotate( dir )
    Array.prototype.rotate = function ( dir ) {
        var that = this,
            direction = dir !== "ccw",
            columns = that[0].length,
            flat = [],
            tmp = [];
        if ( !(that.length) || !columns ) {
            console.error("incompatible array lengths");
            return false;
    ...
    
  2. rotate( n )
    Array.prototype.rotate = function( n ) {
      this.unshift.apply( this, this.splice( n, this.length ) )
      return this;
    
  3. rotate(()
    Array.prototype.rotate = (function(){
      var unshift = Array.prototype.unshift,
      push=Array.prototype.push,
      splice=Array.prototype.splice;
      return function(count){
        var len=this.length >>>0,
        count = count >> 0;
        count = count % len;
        push.apply(this,splice.call(this,count,len));
    ...
    
  4. rotate(()
    'use strict';
    Array.prototype.rotate = (function() {
        var unshift = Array.prototype.unshift;
        var 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;
    ...
    
  5. rotate(()
    export function zeroOrOne() {
      return Math.round(Math.random() * 1)
    export function arrayShuffle(a) {
      let j, x, i;
      for (i = a.length; i; i -= 1) {
        j = Math.floor(Math.random() * i);
        x = a[i - 1];
        a[i - 1] = a[j];
    ...
    
  6. rotate(count)
    Array.prototype.rotate = function(count)
            var len = this.length >>> 0, 
                count = count >> 0; 
            count = ((count % len) + len) % len;
            var copy = this.clone(this);
            push.apply(copy, splice.call(copy, 0, count));
            return copy;
      };
    ...
    
  7. rotate(n)
    Array.prototype.rotate = function(n) {
      this.unshift.apply(this, this.splice(n + 1, this.length))
      return this;
    var arr = [1, 2, 3, 4, 5];
    var arr1 = [1, 2, 3, 4, 5];
    var rotate = function(arr, r) {
      arr.unshift.apply(arr, arr.splice(r + 1, arr.length));
    };
    ...
    
  8. rotate(n)
    var a =  (function() { var a = []; for (var i = 0; i < 35; i++) { a.push(i) }; return a; }())
    Array.prototype.rotate = function(n) {
      var a = this.concat(this.splice(0, n))
      return a
    a.rotate(12)
    function findI(arr, i) {
      if (arr.length == 2) { return i+1 }
      var k = (arr.length/2).floor(), i = i || 0
    ...
    
  9. rotate(times)
    Array.prototype.rotate = function(times) {
      if (times < 0) {
        var last = arr.pop
        return arr.unshift(last)
    function solution (numbers, times) {
      for (var i = 0; i < times; i++) {
        numbers.rotate
    ...