I'm trying to build a multi-dimensional array dynamically.
The reason I want to build it dynamically is if the array grows to like 1 - 1000 in 5-number chunks.
It would be very time consuming to write it like this:
[1, 2, 3, 4, 5],,,,,[996, 997, 998, 999, 1000]
I've been struggling the whole day today, so I decided to post this question because I'm totally stuck right now.
This is the array I want to build dynamically (my earlier post which is solved):
Multi-dimensional array shuffle random
Once the dynamic array is built properly, I want to call the fisherYates() function with 'outerArr.forEach(fisherYates);' to get a result like this:
[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]
The array will be used to fadeOut/fadeIn pictures.
1. fadeIn first set of five random pictures 1-5
2. fadeOut first set
3. fadeIn second set of five random pictures 6-10
4. fadeOut second set
5. fadeIn third set of five random pictures 11-15
6. And so on....
I will use the array values like this:
$currImg = $('.rotator-image:visible', $currLi);
$next = $('.img' + outerArr[a][b], $currLi);
$currImg.fadeOut(1000);
$next.fadeIn(1000);
I've tried to solved this with the help of these links:
jQuery.map
Create multidimentional array with dynamic form
Pointy's code in this post.
Some notes: I prefer not to use the "var outerArr = new Array();". I read somwhere that it should be avoided(?). I would like to accomplish this the jQuery way, with .push and $.makeArray (and $.map) if that's possible. However, any approach is appreciated.
Here is the code I have (increase the Javascript window in JSfiddle to see my comments): (and also here)
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 outerArr = [];
var innerArr = [];
var fakeArr = 0;
var zz = 0;
for (var z = 1; z < 26; ++z) {
++zz;
if (zz != 5) {
fakeArr = fakeArr + z + ",";
} else {
fakeArr = fakeArr + z;
var realArr = $.makeArray(fakeArr);
innerArr.push(realArr);
outerArr.push(innerArr);
innerArr = [];
innerArr.length = 0;
fakeArr = "";
fakeArr.length = 0;
zz = 0;
}
}
// Shuffle/Randomize the numbers in each chunk but not the order of the five chunks
outerArr.forEach(fisherYates);
alert(outerArr);
The problem is when I want to get the values from the array. I don't get the single values (like outerArr[1][3] should show 9). I only get each full chunk (like 6,7,8,9,10). I belive I have to use $.map but I don't know how to use $.map for my example. The shuffle/random function (i.e. outerArr.forEach(fisherYates);) doesn't work either as the code is right now.
The array should also be randomized (as explained in the first link at the top) but I should be able to get the shuffle/random working once I get the dynamic part working.
$.makeArray(fakeArr)isn't doing what you seem to think it is. It expects an object as input, not a string.