Last active
September 16, 2016 15:26
-
-
Save danbrianwhite/03b2f52bdbfa977c1fdb to your computer and use it in GitHub Desktop.
parallelShuffleArrays shuffleArrayOfArrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parallelShuffleArrays(arrays) | |
{ | |
var itemsLeft; | |
var selectedItemIndex; | |
var temporaryItem; | |
var array; | |
function useObject() | |
{ | |
//check to see if all arrays are equal lengths | |
itemsLeft = getNumberItemsObject(arrays); | |
//if the arrays are equal lengths and while there are still items to shuffle | |
while(itemsLeft) | |
{ | |
selectedItemIndex = ~~(Math.random() * itemsLeft--); | |
for (property in arrays) | |
{ | |
array = arrays[property]; | |
temporaryItem = array[itemsLeft]; | |
array[itemsLeft] = array[selectedItemIndex]; | |
array[selectedItemIndex] = temporaryItem; | |
}; | |
} | |
} | |
function useArray() | |
{ | |
//check to see if all arrays are equal lengths | |
itemsLeft = getNumberItemsArray(arrays); | |
//if the arrays are equal lengths and while there are still items to shuffle | |
while(itemsLeft) | |
{ | |
selectedItemIndex = ~~(Math.random() * itemsLeft--); | |
for (var i = 0; i < arrays.length; i++) { | |
array = arrays[i]; | |
temporaryItem = array[itemsLeft]; | |
array[itemsLeft] = array[selectedItemIndex]; | |
array[selectedItemIndex] = temporaryItem; | |
}; | |
} | |
} | |
var checkObjectOrArray = Object.prototype.toString.call(arrays); | |
if(checkObjectOrArray === "[object Object]") | |
{ | |
useObject(); | |
} | |
else if(checkObjectOrArray === "[object Array]") | |
{ | |
useArray(); | |
} | |
} | |
function shuffleArrayOfArrays(arrays) | |
{ | |
var items = getNumberItemsArray(arrays); | |
function createArrayList(arr, index) | |
{ | |
var rtnArr = new Array(); | |
for(property in arr) | |
{ | |
rtnArr.push(arr[property][index]) | |
} | |
return rtnArr; | |
} | |
for (var i = items - 1; i >= 0; i--) | |
{ | |
parallelShuffleArrays(createArrayList(arrays, i)); | |
}; | |
} | |
function getNumberItemsArray(arrays) | |
{ | |
var itemsLeft; | |
for (var i = arrays.length - 1; i >= 0; i--) | |
{ | |
if (typeof(itemsLeft) === "undefined") | |
{ | |
itemsLeft = arrays[i].length; | |
} | |
else if (itemsLeft > arrays[i].length) | |
{ | |
itemsLeft = arrays[i].length; | |
} | |
}; | |
return itemsLeft; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment