Skip to content

Instantly share code, notes, and snippets.

@isRuslan
Created April 1, 2015 21:44

Revisions

  1. isRuslan created this gist Apr 1, 2015.
    12 changes: 12 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /**
    * Write a function that takes an array of integers
    and returns that array rotated by N positions.
    For example, if N=2, given the input array [1, 2, 3, 4, 5, 6]
    the function should return [5, 6, 1, 2, 3, 4]
    */
    var rotate = function (arr, n) {
    var L = arr.length;
    return arr.slice(L - n).concat(arr.slice(0, L - n));
    };

    console.assert( rotate( [1, 2, 3, 4, 5, 6] ), [5, 6, 1, 2, 3, 4] );