davi
davi

Reputation: 13

actionscript number is between():Boolean

Does Actionscript have a built-in function that accepts a number and can return a Boolean if this number is between 2 numbers.

For example

3 is between 2 and 6   //returns true
5 is between 10 and 20 //returns false

Upvotes: 1

Views: 227

Answers (2)

ansiart
ansiart

Reputation: 2571

function calls are pretty slow, so I would stay away from this if you can at all avoid it.

It's not that hard to write: if (x > low && x < high)

Upvotes: 0

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95519

No, but you can easily code one yourself:

public static function isBetween(x : Number, low: Number, high : Number) : Boolean {
    return ((x>=low)&&(x<=high));
}

So, for your example, isBetween(3,2,6) returns true and isBetween(5,10,20) returns false. That said, simply using the boolean expression ((x>=2)&&(x<=6)) is much more readable than isBetween(x,2,6).

Upvotes: 5

Related Questions