Write rotate(array, k) that rotates an array k steps to the right.
Return a new array and leave the input alone. A negative k rotates to the left, and k can be larger than the array's length. k is always an integer.
function rotate(array, k) {
// your code
}
// rotate([1, 2, 3, 4, 5], 2); // [4, 5, 1, 2, 3]
// rotate([1, 2, 3, 4, 5], -1); // [2, 3, 4, 5, 1]
// rotate([1, 2, 3], 4); // [3, 1, 2]
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…