# Checking values in an array. Anyone?

**URL:** <https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369>\
**Category:** Uncategorized\
**Created:** [May 14, 2008, 8:26pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369 "2008-05-14T20:26:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpearson311](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jpearson311](https://forum.kirupa.com/u/jpearson311)\
**Post date:** [May 14, 2008, 8:26pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/1 "2008-05-14T20:26:13Z")

</div>

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:

```auto

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:

```auto

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!

---

<div class="post-metadata">

**Author:** ![rmo518](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rmo518](https://forum.kirupa.com/u/rmo518)\
**Post date:** [May 14, 2008, 8:33pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/2 "2008-05-14T20:33:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jpearson311](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jpearson311](https://forum.kirupa.com/u/jpearson311)\
**Post date:** [May 14, 2008, 8:44pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/3 "2008-05-14T20:44:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Valaran](https://avatars.discourse-cdn.com/v4/letter/v/ee59a6/32.png) [@Valaran](https://forum.kirupa.com/u/Valaran)\
**Post date:** [May 15, 2008, 2:03am UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/4 "2008-05-15T02:03:11Z")

</div>

Something like… this perhaps:

```auto

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;
            }
        }
    }
};

```

---

<div class="post-metadata">

**Author:** ![jpearson311](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jpearson311](https://forum.kirupa.com/u/jpearson311)\
**Post date:** [May 15, 2008, 5:30pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/5 "2008-05-15T17:30:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 15, 2008, 7:11pm UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/6 "2008-05-15T19:11:45Z")

</div>

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

```auto

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:

```auto
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:

```auto
// 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:

```auto
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();
};

```

---

<div class="post-metadata">

**Author:** ![rmo518](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rmo518](https://forum.kirupa.com/u/rmo518)\
**Post date:** [May 16, 2008, 1:04am UTC](https://forum.kirupa.com/t/checking-values-in-an-array-anyone/260369/7 "2008-05-16T01:04:57Z")

</div>

> [@jpearson311;2326309](#):
>
> 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?

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.
