Sunday, January 13, 2013

Function to Reshuffle a JavaScript Array

Here is a JavaScript function I created in order to reshuffle the elements in an Array. Feel free to use, modify and distribute.

function reshuffle(x)
 {
 // x is the array to reshuffle
 var y = new Array();
 while(x.length!=0)
  {
  var i = Math.floor(Math.random() * x.length);
  y.splice(0,0,x[i]);
  x.splice(i,1);
  }
 return y; // RESULTING ARRAY
 }

No comments:

Post a Comment

Did this help you?
How can we help you further?