Shuffle array elements and display randomly using javascript

by Nirmala 2008-10-13 12:04:59

Hi...

Use the following code to shuffle the array elements and display randomly.

<script type="text/javascript">
shuffle = function(o)
{
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var arr =new Array(1,2,3,4,5,6,7);
var shuff=shuffle(arr);
alert(arr);
</script>

Tagged in:

2255
like
1
dislike
0
mail
flag

You must LOGIN to add comments