I have an array like [1,2,3,4,5] and i have the total number to rotate the array like 3. So, after the first rotation the array should be [2,3,4,5,1]. This way the array should keep rotating n number of times which is given. I have a solution to rotate the array which is shown below but i can't do this n number of times. Here's what i have done :
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);
ntimes?newArr[i-1]=arr[i]and then after each iteration setarr = [...newArr]