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 :)

2
  • Create an array of arrays and choose one of them randomly. See: Getting random value from an array. Whenever you have a collection of something, use an array or object to manage it. Commented May 3, 2012 at 9:22
  • possible duplicate of Select random function Commented May 3, 2012 at 9:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Man you just solved my problem! Thank you! So yes, if people are now having the same problem as me - take Zvonas answer.
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)];

2 Comments

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<theArray.length;hits++){ if(theArray[hits]==obj){ j=true; break; } } return j; } Because if I do that, the buttons are no longer able to be clicked :/
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.