<label for="employees">Employees</label>
<textarea id="employees">
Autumn
Brent
Bryan
Candice
Christy
Heather
Jay
Nicole
Laoleng
Derrick
Jamie
Zane
Avery
Bob
Christopher
Dillon
Hayden
Jennifer
Jenny
Jessica
Josh
Judith
Kari
Mason
Matthew
Ryan
Savannah
Sherrill
Tim
Ward
Andrew
Kishor
Misty
Joshua
Jeffrey
Mark
Sean
Troy
Jacob
John
Mike
Stephanie</textarea>
<br/>
<button id ="shuffle" type="button">Shuffle!</button>
<br/>
<br/>
<ul id=alexList>
</ul>
<ul id=mattList>
</ul>
<ul id=justinList>
</ul>
CSS
ul {
float: left;
}
JavaScript
$( "#shuffle" ).click(function() {
var employeeList = document.getElementById("employees").value.split(/\r?\n/);
var shuffledEmployees = shuffleArray(employeeList);
var chunkedArray = chunkify(shuffledEmployees,3);
$('#alexList').empty()
$('#mattList').empty()
$('#justinList').empty()
for(var i = 0; i < chunkedArray.length; i++)
{
for(var x = 0; x < chunkedArray[i].length; x++)
{
if(i == 0)
{
setValue(chunkedArray[i][x], "alexList");
}
if(i == 1)
{
setValue(chunkedArray[i][x], "mattList");
}
if(i == 2)
{
setValue(chunkedArray[i][x], "justinList");
}
}
}
});
function setValue(val, list) {
var ul = document.getElementById(list);
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;
}
function chunkify(a, n, balanced) {
if (n < 2)
return [a];
var len = a.length,
out = [],
i = 0,
size;
if (len % n === 0) {
size = Math.floor(len / n);
while (i < len) {
out.push(a.slice(i, i += size));
}
}
else if (balanced) {
while (i < len) {
size = Math.ceil((len - i) / n--);
out.push(a.slice(i, i += size));
}
}
else {
n--;
size = Math.floor(len / n);
if (len % size === 0)
size--;
while (i < size * n) {
out.push(a.slice(i, i += size));
}
out.push(a.slice(size * n));
}
return out;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.