Mind has gone blank this afternoon and can't for the life of me figure out the right way to do this:
if(i!="3" && i!="4" && i!="5" && i!="6" && i!="7" && i!="8" && i!="9" && i!="2" && i!="19" && i!="18" && i!="60" && i!="61" && i!="50" && i!="49" && i!="79" && i!="78" && i!="81" && i!="82" && i!="80" && i!="70" && i!="90" && i!="91" && i!="92" && i!="93" && i!="94"){
//do stuff
}
All those numbers need to be in an array, then I can check to see if "i
" is not equal to any 1 of them.
The Array.find() method returns the value of the array element that passes a test (provided by a function). The method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
If the element found, the flag value will change inside the if condition and that’s how we can check whether it is present or not. Now let’s consider another method of checking if the element is present in the array or not. It's the includes () method, which is now commonly used.
So the find () method returns the first element inside an array which satisfies the callback function. The callback function can take in the following parameters: currentItem: This is the element in the array which is currently being iterated over. index: This is the index position of the currentItem inside the array.
Well, we can use the find () method to do just that. The find () method is an Array.prototype (aka built-in) method which takes in a callback function and calls that function for every item it iterates over inside of the array it is bound to.
var a = [3,4,5,6,7,8,9];
if ( a.indexOf( 2 ) == -1 ) {
// do stuff
}
indexOf
returns -1
if the number is not found. It returns something other than -1
if it is found. Change your logic if you want.
Wrap the numbers in quotes if you need strings ( a = ['1','2']
). I don't know what you're dealing with so I made them numbers.
IE and other obscure/older browsers will need the indexOf
method:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With