Nodejs Array Rotate rotate(n)

Here you can find the source of rotate(n)

Method Source Code

var a =  (function() { var a = []; for (var i = 0; i < 35; i++) { a.push(i) }; return a; }())
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]

Array.prototype.rotate = function(n) {
  var a = this.concat(this.splice(0, n))
  return a//  www.ja  v  a  2  s.  c  om
}

a.rotate(12)
// [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

function findI(arr, i) {
  if (arr.length == 2) { return i+1 }
  var k = (arr.length/2).floor(), i = i || 0
  if (arr[0] > arr[k]) { 
    return findI(arr.slice(0, k+1), i)
  } else {
    return findI(arr.slice(k), k+i)
  }

}

Related

  1. 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;
    ...
    
  2. 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];
    ...
    
  3. rotate(K)
    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;
    ...
    
  4. 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;
      };
    ...
    
  5. 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));
    };
    ...
    
  6. 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
    ...
    
  7. rotateArray(n)
    Array.prototype.rotateArray = function (n) {
        let firstArray = [];
        let secondArray = [];
        for (let i = this.length - 1; i > this.length - n - 1; i--) {
            firstArray.unshift(this[i]);
        for (let j = 0; j < this.length - n; j++) {
            secondArray[secondArray.length] = this[j];
        return firstArray.concat(secondArray);
    function createArray() {
        let valuesStr = prompt("Enter an Array : ");
        let arrayStr = valuesStr.split(" ");
        let array = [];
        for (let i = 0; i < arrayStr.length; i++) {
            if (arrayStr[i] )
            array[array.length] = Number(arrayStr[i]);
        return array;
    let array = createArray();
    console.log(array.rotateArray(3));
    
  8. rotate_copy(begin, middle, end)
    Array.prototype.rotate_copy = function (begin, middle, end) {
      var back = this.slice (begin, middle - begin);
      back.unshift (0);
      back.unshift (end - middle);
      var front = this.slice (middle, end);
      front.splice.apply (front, back);
      return front;
    };