DEV Community

Discussion on: Array Rotation in JS

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

Also Number.parseInt(arr.length % d) could be useful in case the number is greater than the arr length.

const completeRotations = arr.length / d;

let extraSteps = Number.parseInt(arr.length % d);
Enter fullscreen mode Exit fullscreen mode

With that you can simply do a single complete rotation and keeo going the extra steps instead of failing 😁

Collapse
 
shubhamtiwari909 profile image
Shubham Tiwari

Thank you i will try this as well 😉