Search code examples
actionscript-3

Replacing array with another array but same name


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.


Solution

  • 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");
    }