AS3 Put Last Item of Array at the Beginning


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function putLastItemOfArrayAtBeginning($a:Array):Array
  2. {
  3. $a.unshift($a[$a.length-1]);
  4. $a.pop();
  5. return $a;
  6. }
  7.  
  8. var myArray:Array = new Array(0,1,2,3,4,5);
  9. trace(myArray);
  10. myArray = putLastItemOfArrayAtBeginning(myArray);
  11. trace(myArray);
  12. myArray = putLastItemOfArrayAtBeginning(myArray);
  13. trace(myArray);
  14. myArray = putLastItemOfArrayAtBeginning(myArray);
  15. trace(myArray);
  16.  
  17. // OUTPUT: 0,1,2,3,4,5
  18. // OUTPUT: 5,0,1,2,3,4
  19. // OUTPUT: 4,5,0,1,2,3
  20. // OUTPUT: 3,4,5,0,1,2

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.