I am trying to randomize a Javascript array containing HTML elements. Here's what I have so far, but unfortunately I still get several duplicates afterwards. I don't see what's wrong about this snippet of code. Any ideas, anyone?
function randomizeArray(theArray)
{
var result = new Array();
var randomNr;
while ( theArray.length>0 )
{
randomNr = Math.floor( (Math.random() * theArray.length) );
result.push( theArray[randomNr] );
theArray.splice( randomNr, 1);
}
return result;
}
Edit: After further review, this code appeared to work as intended when I tested it with an array of numbers. For some reason I still got duplicates with my array of