1

Alright, so the thing is;

Now I made this website for myself to study japanese more easily, by making a list of japanese words and its definition. I put the japanese words in lesson1.txt and the answers in lesson1answers.txt in the same order. I read them out in php and put them in arrays in javascript.

Now the problem is is that these arrays have the same order every time I start the site, which is quite annoying for me as I keep resembling the spot to the answer, and I dont even read the japanese word (Damn our human brains!). So I figured the best way to solve this is by randomizing the elements of both the arrays, but they both have to be randomized in the same pattern because else the question doesnt correspond correctly to the answer anymore.

One of the arrays I have is multidimensional. The array first holds elements that represent the lines I read out of a .txt file. The line has multiple characters in it, consequently the second element of the array represents each character (Dont ask me why I did this, it had to do with me not being able to read out japanese characters in php and put them in js directly). The other array is just the array I keep the answers in.

So I have AnswersArray(); and JapaneseCharactersArray();

I've been stuck on this problem for a whole day now, someone please help or give a hint..

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
user1534664
  • 3,258
  • 8
  • 40
  • 66
  • 1
    Could you show us a sample array. I am not visioning what you are saying. – frosty Jul 19 '12 at 22:34
  • So the AnswersArray is in english so that's why you're using a non-multi-dimensional array. What's the indexes of the Japanese Characters Array()? What I mean is it layed out something like JapaneseCharactersArray('325235235')(0)='3', JapaneseCharactersArray('325235235')(1)='2', – jeschafe Jul 19 '12 at 22:38
  • JapaneseCharactersArray[0][0] = お JapaneseCharactersArray[0][1] = お JapaneseCharactersArray[0][2] = き JapaneseCharactersArray[0][3] = い – user1534664 Jul 19 '12 at 22:41
  • @user1534664: Reading that script you linked hurts :-| Please ask another question on how to improve the array output, containing your php code. Evaling Unicode-codepoint-strings to get characters is *not* the way to go. – Bergi Jul 19 '12 at 22:44
  • This is only my second year programming, mate. I'm sorry my skills arent that good yet. But thats besides the point, I just want it working right now. – user1534664 Jul 19 '12 at 22:49

2 Answers2

3

You should merge them into one array of objects:

// assuming AnswersArray.length == JapaneseCharactersArray.length
var array = [];
for (var i=0; i<AnswersArray.length; i++)
    array[i] = {
       answer: AnswersArray[i],
       japanese: JapaneseCharactersArray[i]
    };

Now you have only one array, each item containing all information on a single word. You can easily shuffle it now.

Instead of AnswersArray[x] you then would use array[x].answer further down in your code.


Of course, you could just adapt your fisherYates function to shuffle two or more arrays in the same way:

function fisherYates(first/*, ... arrays */) {
    var i = first.length,
        argl = arguments.length;
    if (i == 0) return false;
    while (--i) {
        var r = Math.floor(Math.random() * (i + 1));
        for (var j=0; j<argl; j++) {
            var tempi = arguments[j][i];
            var tempj = arguments[j][r];
            arguments[j][i] = tempj;
            arguments[j][r] = tempi;
        }
    }
}

// usage:
> fisherYates(AnswersArray, JapaneseCharactersArray);
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks mate, I am not very familiar with javascript yet so I'ma try this out. This does seem like a good solution. – user1534664 Jul 19 '12 at 22:42
  • the top solution still seems the best because its alot clearer to me ;) Thanks for the effort and fast reply. – user1534664 Jul 19 '12 at 22:58
0

Although the best way is probably to just create them as a single array, another way is to create a new array, of the same length as the others, where each item in the array is a number: 1, 2, 3 ... n. But instead of having the numbers in order, use Math.random() to randomize their order.

Then loop through that new array as such:

for(i=0; i<AnswersArray.length; i++) {
    AnswersArray[RandomArray[i]]; // whatever
}

Another way is to just swap the entire row when you randomize the array.

cegfault
  • 6,442
  • 3
  • 27
  • 49