I want to minimize my code:
myArrayA = [1, 2, 3, 4, 5]; fisherYates(myArrayA); myArrayB = [6, 7, 8, 9, 10]; fisherYates(myArrayB); myArrayC = [11, 12, 13, 14, 15]; fisherYates(myArrayC); myArrayD = [16, 17, 18, 19, 20]; fisherYates(myArrayD); myArrayE = [21, 22, 23, 24, 25]; fisherYates(myArrayE);
To:
var multArr = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]; fisherYates(multArr);
The output I want is as follows:
[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]
I tried this code:
http://jsfiddle.net/arrow/yFn8U/
function fisherYates(myArray) { var i = myArray.length, j, tempi, tempj; if (i === 0) return false; while (--i) { j = Math.floor(Math.random() * (i + 1)); tempi = myArray[i]; tempj = myArray[j]; myArray[i] = tempj; myArray[j] = tempi; } } var multArr = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]; fisherYates(multArr);
But my code only randomizes the order of the pieces, not the values ββin each fragment.
The result I want is as follows:
[4,2,3,5,1],[7,10,6,9,8],[11,15,12,14,13],[18,17,16,20,19],[22,21,25,23,24]
I want each piece inside the array to be in the same order, but each fragment must be randomized.
Is there a way to do this using jQuery?
I also wonder how to get values ββfrom a shuffled / randomized array?
At the moment, I get the following values:
myArrayA[i] myArrayB[i] myArrayC[i] myArrayD[i] myArrayE[i]
I would suggest that I get them with something like:
multArr [[0][i]]; multArr [[1][i]]; multArr [[2][i]]; multArr [[3][i]]; multArr [[4][i]];
Finally, I wonder if code minimization will work better?