Advertisement
PowerCell46

Array rotation JS

Nov 15th, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayRotation(array, numberOfRotations) {
  2.  
  3. let newStartingArray = [];
  4. let leftoutArray = [];
  5.  
  6. while (numberOfRotations> Number(array.length)) {
  7. numberOfRotations -= Number(array.length);
  8. }
  9.  
  10. for (let index = 0; index < Number(array.length); index++) {
  11.  
  12. if (index !== numberOfRotations) {
  13. leftoutArray.push(array[index]);
  14.  
  15. } else if (index === numberOfRotations) {
  16. newStartingArray.push(array[index]);
  17.  
  18. while (index < Number(array.length)) {
  19. index++;
  20. newStartingArray.push(array[index]);
  21. }
  22. }
  23. }
  24.  
  25. newStartingArray = newStartingArray.join(" ");
  26. leftoutArray = leftoutArray.join(" ");
  27. console.log(newStartingArray + leftoutArray);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement