Array Shuffle

Basic array shuffler function using Fisher-Yates shuffle mechanic

by TokenEx Support

HTML

<ul id=employeeList>

</ul>

JavaScript

var employeeList = ["asdf","asdddd","bbbb","cccc"];

var employeeList = shuffleArray(employeeList);

setValue(employeeList[0]);

for(var i = 0; i < employeeList.length; i++)
{
 setValue(employeeList[i]);
}


function setValue(val) {
  var ul = document.getElementById("employeeList");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(val));
  ul.appendChild(li);
}
//console.log(shuffleArray(array)));

/** FUNCTION
 * Randomize array element order in-place.
 * Using Fisher-Yates shuffle algorithm.
 */
function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}