1

How do I replace an array or define it and then give it values based off a users choice? The code with the if statements below are some choices I need in one or both arrays.

For example

if(choice=="easy")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom");
}
if(choice=="hard")
{
var sorted:Array = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}

But this doesn't work above.

2 Answers 2

1

Declare the variable outside the condition instead (I also changed if/if to if/elseif as choice can't be both easy and hard at the same time):

var sorted:Array;

if(choice=="easy")
{
    sorted = new Array("Beau","Dad","Jesus","Mary","Mom");
} else if(choice=="hard")
{
    sorted = new Array("Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley","Daddy","Mommy","Grandma","Grandpa","Nana","Gepa");
}
Sign up to request clarification or add additional context in comments.

Comments

1

As an additional option to h2oooooo' answer , you can use something like:

    var sorted:Array = {    
              "easy":["Beau","Dad","Jesus","Mary","Mom"],
              "medium":["Jordyn","Presley","Jesus","Mary","Nana"],
              "hard":["Beau","Dad","Jesus","Mary","Mom","Jordyn","Presley",
                      "Daddy","Mommy","Grandma","Grandpa","Nana","Gepa"]
               }[choice];

    trace(sorted.constructor); // [class Array]

2 Comments

@h2ooooooo , for sceptics i attached a constructor' trace ;)
Ah, ignore my previous comment if you saw it - I just thought it was odd creating an object, but assigning it to an Array as I missed the [choice] part :)

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.