vikkktor

Arrays_rotateArray

Oct 13th, 2021
891
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //------------------------------------------------------
  2. //Write a function that rotates an array.
  3. //The array should be rotated to the right side, meaning that the last element should become the //first, upon rotation.
  4. //The input comes as array of strings. The last element of the array is the amount of rotation you need to perform.
  5. //The output is the resulted array after the rotations. The elements should be printed on one line, separated by a single space.
  6. //------------------------------------------------------
  7.  
  8. function rotate_Array(array) {
  9.     let arr = array;
  10.  
  11.     let rotations = arr.pop()
  12.     for (i = 0; i < rotations; i++) {
  13.         let lastEl = arr.pop()
  14.         arr.unshift(lastEl)
  15.     }
  16.     console.log(arr.join(' '))
  17. }
RAW Paste Data