This is a cut-and-dried Fisher-Yates implementation. As not typically a JS developer, I've tried to incorporate as many best practices as I can. I would appreciate any input as to make it conform to more best practices and continue to be maintainable.
(function fisherYates () {
"use strict";
var shuffle = function shuffle (array) {
var beginningIndex = 0,
currentIndex = array.length,
indexDecrement = 1,
randomIndex = 0,
temporaryValue = 0;
// While elements remain to be shuffled...
while (currentIndex !== beginningIndex) {
// Pick an element from the remaining elements...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= indexDecrement;
// Swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
return shuffle;
}());