List of utility methods to do Array Rotate
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 ... | |
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 ... | |
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)); | |
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; }; |