Switch to full style
JavaScript code examples
Post a reply

Shuffle Array

Mon Sep 19, 2011 12:17 pm

Shuffle and traversing array.You need to refresh to notice the shuffling.
Code:

<html>
<
title>Shuffle Array</title>
<
head>
<
script language="JavaScript">
function Shuffle(arr) {
    for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), 
    x 
= arr[--i], arr[i] = arr[j], arr[j] = x);
    return arr;
};

function ShuffleArray()
{
    var colorArr= Array("RED", "BLUE", "YELLOW");
    suffcolor=Shuffle(colorArr);
    // traverse array
    for (key in suffcolor)
    document.write("<br/>color value is " + suffcolor[key]);
}
 

</script>
</head>

<body>

 
<SCRIPT language="JavaScript">
 
ShuffleArray();
 
</SCRIPT>

</body>
</HTML>





Post a reply
  Related Posts  to : Shuffle Array
 Array shuffle     -  
 Array difference for associate array     -  
 compare an array with another array?     -  
 Array size to zero     -  
 Add elements to the end of an array     -  
 Get array chunk     -  
 Array sort     -  
 C++ array copying     -  
 Pad array to the specified length with a value     -  
 XML Document into an Array in php     -  

Topic Tags

JavaScript Arrays