Posted By

arpo on 03/14/11


Tagged


Versions (?)

Shift array left and right


 / Published in: JavaScript
 

Take the first item in an array an place it last or the last item and place it first.

  1. function shiftRight(arr)
  2. {
  3. var temp = new Array();
  4. temp.push(arr[arr.length-1]);
  5. for (i = 0; i <= arr.length-2; i++) temp.push(arr[i]);
  6. return temp;
  7. }
  8.  
  9. function shiftLeft(arr)
  10. {
  11. var temp = new Array();
  12. for (i = 1; i <= arr.length-1; i++) temp.push(arr[i]);
  13. temp.push(arr[0]);
  14. return temp;
  15. }

Report this snippet  

You need to login to post a comment.