Rotate an array
MediumArrays · JavaScript
The task
Write rotate(arr, k) that returns a new array rotated to the right by k steps, so the last k items move to the front.
k can be larger than the length of the array. An empty array stays empty.
Examples
- rotate([1, 2, 3, 4, 5], 2) returns [4,5,1,2,3]
Your code runs in your browser, in a separate worker, and is never sent to us. Edit the function, then press Run tests. Your work is saved here as you type.
JavaScriptCtrl or Cmd + Enter to run