Javascript Array rotate(n)
Array.prototype.rotate = function (n) { if (n > 0) { return this.slice(n).concat(this.slice(0, n)) } else {/*from w w w .ja v a 2 s . com*/ return this.slice(this.length + n).concat(this.slice(0, this.length + n)) } } console.log("hello mlbam team");
Array.prototype.rotate = function(n) { this.unshift.apply(this, this.splice(n + 1, this.length)) return this;//ww w . j ava 2 s . c om } var arr = [1, 2, 3, 4, 5]; var arr1 = [1, 2, 3, 4, 5]; /* * Implement a function rotateArray(vector<int> arr, int r) which rotates the array by r places. Eg 1 2 3 4 5 on being rotated by 2 gives 4 5 1 2 3. */ var rotate = function(arr, r) { arr.unshift.apply(arr, arr.splice(r + 1, arr.length)); }; rotate(arr, 2); console.log(arr); // arr1.rotate(2); // console.log(arr1);