Skip to content Skip to sidebar Skip to footer

Order Two Arrays Based On The Same Random Sorting System

My current Project is a little Jquery Game. Every picture in the Array Bildquelle (which I saved in a json) belongs to a number in the Array numbers, but in the same order, so that

Solution 1:

Probably the easiest is to just not use numbers but directly shuffle Bildquelle instead:

  Bildquelle.sort(function() {
    return Math.random() - 0.5;
  });

Now Bilquelle[0] might be "../img/Bild9.png" or one of the others.

If you however want to keep Bildquelle and numbers and shuffle numbers you should change numbers to be zero based (as arrays):

var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

You could even directly get Bildquelle's keys:

var numbers = Object.keys(Bildquelle);

Now you can shuffle that, and access the Bildquelle by a certain number:

  Bildquelle[ numbers[0] ]

or in your loop as:

  Bildquelle[ numbers[i] ]

PS: Bildquelle should actually be images or pictures to follow naming conventions (and if you keep mixing languages it gets harder to read...)

Post a Comment for "Order Two Arrays Based On The Same Random Sorting System"