I was wondering what was the most efficient way to rotate a JavaScript array.
I came up with this solution, where a positive n
rotates the array to the
** Using Latest version of JS we can build it every easily **
Array.prototype.rotateLeft = function (n) {
this.unshift(...this.splice(-(n), n));
return this
}
here moves: number of rotations ,a Array that you can pass random number
let a = [1, 2, 3, 4, 5, 6, 7];
let moves = 4;
let output = a.rotateLeft(moves);
console.log("Result:", output)