6 Replies Latest reply on Jul 16, 2006 8:17 AM by Newsgroup_User

# A simple for loop question

Hi

Lets say I have an array with boolean values in it:
my_array = [ false , false , false ]

My question is:
How do you make a function to check, if the value is all false, than do this (not only one but all false than trigger a function)

thank you
• ###### 1. A simple for loop question
Alright this might be what you were looking for. try it out
• ###### 2. Re: A simple for loop question
The 350Z,

> Lets say I have an array with boolean values in it:
> my_array = [ false , false , false ]

I'm with ya.

> My question is:
> How do you make a function to check, if the value is
> all false, than do this (not only one but all false than
> trigger a function)

handy. :) In your case, you're looking for all three values to be false.
If even a mere one of them is true, the whole result doesn't count. Let's
write a function that returns true if all three are false, and returns false
if any are true.

function checkWholeArray(arr:Array):Boolean {
for (var n:Number = 0; n < arr.length; n++) {
if (arr[n] == true) {
return false;
}
}
return true;
}

To use this function, you could call it and supply your array as the
parameter ...

checkWholeArray(my_array);

... and since that resolves to either true or false, you could even use that
expression in an if statement.

if (checkWholeArray(my_array)) {
// do something
} else {
// do something else
}

The for loop simply steps through each element in the passed-in array.
If the current element is true, then the desired outcome -- that all
elements are false -- is a loss, so the function returns false and
immediately stops (the return statement always exits the function at that
point). Otherwise, the for loop finishes, and the function returns true.

David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 3. Re: A simple for loop question
Hi all.. thanks for the help, I really appreciate.

David,
what does return false means? what does it do?
So you can insert an array in a function parameter.. like checkWholeArray(my_array) ?

thanks
• ###### 4. Re: A simple for loop question
Thanks fishkin thats working just perfect.. thanks alot :) you're awesome !! :D
cheers :D

David, if possible I still want to know what does that return false and return true do :) thanks
• ###### 5. Re: A simple for loop question
What .. there is nothing here..

"fikshin" <webforumsuser@macromedia.com> wrote in message
news:e96vh4\$2qt\$1@forums.macromedia.com...
> Alright this might be what you were looking for. try it out

• ###### 6. Re: A simple for loop question
The 350Z,

> David, if possible I still want to know what does that
> return false and return true do :) thanks

The return statement does two things: a) terminates a function at the
point it is executed and b) causes the function to issue the returned value.
Many functions (and their class counterparts, methods) return values. Some
return strings, such as String.substr().

var str:String = "Flash";
trace(str.substr(2, 3));

If you paste those two lines into a keyframe of a new FLA, you'll see
"ash" in the Output panel. The reason for this is because the
String.substr() method returns a string (the string "ash," in this case).

Some return numbers, such as Math.round();

var num:Number = 1.5;
trace(Math.round(num));

If you test those two lines, you'll see 2 in the Output panel, because
Math.round() returns a number (the number 2, in this case).

In the example I suggested to you, I had the function return a Boolean
(true/false) value. Same idea, except this function returns a Boolean. So
to answer your question, yes, you could reference that function in an if()
statement, because the function returns a value that if() understands.

David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."