Advertisement
vladovip

JS - Array Rotation

Jan 25th, 2022
1,046
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arr, rotations){
  2.  
  3.     rotations %= arr.length;
  4.    
  5.     while(rotations > 0){
  6.    
  7.         let firstEl = arr[0];
  8.    
  9.         for(let i = 0; i < arr.length - 1; i++){
  10.             arr[i] = arr[i+1];
  11.         }
  12.  
  13.         arr[arr.length - 1] = firstEl;
  14.  
  15.     rotations --;
  16.     }
  17.     console.log(arr.join(' '));
  18. }
  19.  
  20. solve([51, 47, 32, 61, 21], 2);
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement