Imagine you have an array like [1,2,3,4,5]
, and you want to rotate it a total of 3
times. After the first rotation, the array should look like [2,3,4,5,1]
. You need a solution that can handle rotating the array any number of times without manual intervention. Here's an attempt at creating a function to rotate the array:
function rotateLeft(arr, n) {
var newArr = [];
for( let i=1; i< arr.length; i++){
newArr.push(arr[i]);
}
newArr.push(arr[0]);
console.log(newArr);
}
rotateLeft([1,2,3,4,5], 3);