svetlozar_kirkov

Rotate Array [JS]

Oct 2nd, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rotateArray(array, direction, times) {
  2.     let arrayResult = array.slice(0);
  3.     if (direction === "left") {
  4.         for (let i = 0; i < times; i++) {
  5.             let taken = arrayResult.shift();
  6.             arrayResult.push(taken);
  7.         }
  8.     }
  9.     else if (direction === "right") {
  10.         for (let i = 0; i < times; i++) {
  11.             let taken = arrayResult.pop();
  12.             arrayResult.unshift(taken);
  13.         }
  14.     }
  15.     else {
  16.         console.log("invalid direction");
  17.     }
  18.     return arrayResult;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment