xxxxxxxxxx
function rotLeft(a, d)
{
let rslt = a.slice(d).concat(a.slice(0,d));
return rslt
}
xxxxxxxxxx
const arr = [1, 2, 3, 4, 5];
function rotateLeft(arr){
//remove the first elemnt of array
let first = arr.shift();
//then add into the end of array
arr.push(first);
return arr;
}
function rotateRight(arr){
//remove the last elemnt of array
let last =arr.pop();
//then add into the first of array
arr.unshift(last)
return arr;
}
xxxxxxxxxx
// Javascript implementation of right rotation
// of an array K number of times
// Function to rightRotate array
function RightRotate(a, n, k)
{
// If rotation is greater
// than size of array
k = k % n;
for (let i = 0; i < n; i++) {
if (i < k) {
// Printing rightmost
// kth elements
document.write(a[n + i - k] + " ");
}
else {
// Prints array after
// 'k' elements
document.write((a[i - k]) + " ");
}
}
document.write("<br>");
}
// Driver code
let Array = [1, 2, 3, 4, 5];
let N = Array.length;
let K = 2;
RightRotate(Array, N, K);
// This code is contributed by gfgking.