say you have (any amount) values in an array and you want to see if any of them is over (lower amount) and under higher amount, how would you do that?
answer without doing a for loop or any lengthy code.
Maybe something like:
var havingParty = false;
if ((theArrayWithValuesIn > 10) && (theArrayWithValuesIn < 100)) {
havingParty = true;
} else {
havingParty = false;
}
would work.
Please note: x
and y
, collision detection, compact code.
If I understand your question correctly, you want to check if a given array has any value greater than some other value.
There is a useful function for this called
some
(Documentation here)An example of using this would be:
A similar function to this is
every
, which checks if all the values fulfil a given condition (Documentation here).An example of this would be:
My examples simply check for greater than, but you could pass in any callback that returns a boolean value for more complicated checks.
So for your example, something like this might do the trick if you want to check that all the elements fill the requirement:
or
if you you only care that at least one element fills the requirement.
According to your condition
Array.some
function will be the most suitable "tool":Here is a simple answer :