0

I am currently creating a game that allows the user to click on numerous amounts of images. Depending on which image they click, different things will happen. I have looked at previous questions and they all seem to ask "How do I randomly select an item inside an array". However, mine is slightly different to these. Sorry if you feel that my answer lies somewhere else. But anyhow!

My question is simply:

How do I randomly select an array? My code so far contains a function that can check whether an integer exists within an array. This is my code so far.

//The array below contains the integers.
example=new Array(1,2,3);


//The function below checks whether 'image' (which is an integer) is the same as any integers within the example array.

function isItThere(obj) {
    var j = false;
    for (var i = 0; i < example.length; i++) {
        if (example[hits] == obj) {
            j = true;
            break;
        }
    }
    return j;
}
//This is the IF statement I have used. After the integer associated with 'image' has been passed through the 'isItThere' function either A or B will happen. (A happens if the number exists).
if(isItThere(image)){

Currently, this all works perfectly fine. Granted it might not be the most efficent way, but it achieves what I have so far wanted.

But I now want to have multiple arrays that contain integers. This is because if the user reloads the game then they know exactly which images to press for them to win. I am therefore wanting to create several arrays and one of which will be selected at random at the beginning of the game.

For example..

example0=new Array(1,2,3);
example1=new Array(4,5,6);
example2=new Array(7,8,9);

I believe I should be using the following code.

var num=Math.floor(Math.random()*3);

And then somehow link that number to the word 'example'.

That way, this part of my code

if(isItThere(image)){

can stay the same, as it is the isItThere that deals with the choosing of a random array.

Hopefully you get what I'm trying to ask for. I have tried to be as descriptive as possible. To summarise once again, I want to be able to select one array at the beginning of the game so that the game can be played multiple times. Can you write the code I need? I have a feeling its very simple. But ive spent days looking.

Thanks for the help :)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Create an *array* of arrays and choose one of them randomly. See: [Getting random value from an array](http://stackoverflow.com/questions/4550505/getting-random-value-from-an-array). Whenever you have a *collection* of something, use an array or object to manage it. – Felix Kling May 03 '12 at 09:22
  • possible duplicate of [Select random function](http://stackoverflow.com/questions/9791853/select-random-function) – Felix Kling May 03 '12 at 09:27

2 Answers2

2

How about making a parent array and then referring to this parent array?

var childArray1 = [1,2,3],
childArray2 = [4,5,6],
childArray3 = [7,8,9],
parentArray = [childArray1, childArray2, childArray3];

You can also add them with parentArray.push(childArray1); , just which one suits you better.

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
  • Man you just solved my problem! Thank you! So yes, if people are now having the same problem as me - take Zvonas answer. – user1371984 May 03 '12 at 10:02
0

You should do an array of arrays, and choose with random :

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

var theArray = myArray[Math.random() * 3)];
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
  • I don't know whether I'm doing something wrong here. But if I were to use your answer. Would I be right in changing my code to function inArray(obj){ var j=false; for(var hits=0;hits – user1371984 May 03 '12 at 09:44
  • Yes it shall work, in fact it's quite the same answer as @zvona but instead of having multiple childArrays, I directly created them in the parent. And my `myArray` is identical as @zvona's `parentArray`. – Cyril N. May 03 '12 at 12:09