Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. /**
  2. * Disorder the array
  3. *
  4. * @param {bool} preserve Returns a copy without modifying the original
  5. * @return {array} The disordered array
  6. */
  7. Array.prototype.disorder = function (preserve) {
  8. var array = preserve ? this.slice() : this;
  9. var disordered = [];
  10.  
  11. while(array.length > 0) {
  12. var index = Math.round(Math.random()*(array.length-1));
  13. disordered.push(array[index]);
  14. array.splice(index, 1);
  15. }
  16.  
  17. if(!preserve) {
  18. Array.prototype.push.apply(this, disordered);
  19. }
  20.  
  21. return disordered;
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement