Nice post! I have two solutions, neither of which is better than yours, but still might be interesting.
Approach 1: Functional style, easy to read but slow
const head = (arr, k) => arr.slice(0, k); const tail = (arr, k) => arr.slice(k); const rotator = (arr, k) => [...tail(arr, k), ...head(arr, k)];
Approach 2: using recursion and modifying the array in place
const recursed = (arr, k) => k == 0 ? arr : (arr.push(arr.shift()) , recursed(arr, --k));
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice post! I have two solutions, neither of which is better than yours, but still might be interesting.
Approach 1: Functional style, easy to read but slow
Approach 2: using recursion and modifying the array in place