Checking values in an array. Anyone?

So I have an array that has 14 values and I use a for loop to initialize all of their values to 0. I then have 14 buttons that when you rollout of each of them, it changes it’s corresponding value in the array to 1. For example:


var myArray:Array = new Array();
myArray[0];
 
for(i = 0; i <= myArray.length; i++){
     myArray* = 0;
}
 
mybutton.onRollOut = function():Void{
     myArray[0] = 1;
};

Now, with that given, all together I have 14 values in my array (myArray[0], myArray[1], etc.) and 14 buttons(1 for each array value). What I want to do is check to see if all of the values in the array are equal to 1 and if they are, trace something. I’m presently doing it like this:


this.onEnterFrame = function():Void{
     for(i = 0; i <= myArray.length; i++){
         if(myArray* == 1){
              trace("It worked!");
             delete onEnterFrame;
         }
     }
};

The problem is that when I do it this way, it traces as soon as I rollout of one button. If you recall, I have 14 buttons and as you roll out of each of them, it’s corresponding array value is set equal to 1. I want it to trace once all of the values in the array are equal to 1.

Any help would be greatly appreciated. Thanks!

Something like this?
[AS]this.onEnterFrame = function():Void{
myString = myArray.join();
if(myString.indexOf(“0”) < 0){
trace(“It worked!”);
delete onEnterFrame;
}
}[/AS]
Convert it to a string and check for a string with no more zeros.

Well, it would actually have to be “greater than” and not “less than” as you stated, but I switched it to greater than and it still traced after rolling out of just one. Thoughts?

Something like… this perhaps:


this.onEnterFrame = function():Void{
    var testNum:Number = 0;
    for(i = 0; i <= myArray.length; i++){
        if(myArray* == 1){
            testNum += 1;
            if(testNum == myArray.length){
                trace("It worked!");
                delete this.onEnterFrame;
            }
        }
    }
};

None of these are working. If anyone else has any idea, I’d appreciate it. There has to be a method or something that can check all of the values in an array, I just don’t know what it is.

[quote=jpearson311;2326296]So I have an array that has 14 values and I use a for loop to initialize all of their values to 0. I then have 14 buttons that when you rollout of each of them, it changes it’s corresponding value in the array to 1. For example:


var myArray:Array = new Array();
myArray[0];
 
for(i = 0; i <= myArray.length; i++){
     myArray* = 0;
}
 
mybutton.onRollOut = function():Void{
     myArray[0] = 1;
};

[/quote]

That method of populating an array leads to an infinite loop - everytime you add a new value to the array, then the array.length increases by one. Thus the for loop never ends.
So use this instead:

var myArray:Array = new Array();
for (i = 0; i <= 13; i++) {
 myArray* = 0;
}

So now you want to check if every value == 1? There are a number of ways to do this, including using Array.toString and then comparing the returned value to “1,1,1,1,1,1,1,1,1,1,1,1,1,1”.

However, a far simpler way is to see if they add up to a certain value, in this case 14. Just loop back through your array, retrieve every value, and add them up. For example:

// create an array containing 14 values of 1 for testing purposes
var myArray:Array = new Array();
for (i = 0; i <= 13; i++) {
myArray* = 1;
}
trace(myArray);
// now loop through the array and add all the values
count = 0;
for (i = 0; i < myArray.length; i++) {
var count += myArray*;
}
// if the value equals 14, every element in the array MUST be a 1
if (count == 14) {
trace("All values are set to 1");
}

You can also softcode the 14 value by substituting if (count = myArray.length) instead.

It should be simple enough now to place this loop and if statement into a function that you can call whenever your button rollovers occur:

function checkArray() {
 var count = 0;
 for (i = 0; i < myArray.length; i++) {
  count += myArray*;
 }
 if (count == myArray.length) {
  trace("It worked!");
 }
}
// code for button
mybutton.onRollOut = function():Void  {
 myArray[0] = 1;
 checkArray();
};

I realize you have some other options at this point, but less than is correct. When a character does not appear in a string it returns an index of -1. So once there are no more zeros the result is less than zero.