javascript - If statement multiple conditionals on one value without repeating value -


i wondering if there way check multiple conditions same variable or value without repeating value.

for example, if had variable keycode, , wanted check if greater 102, or less 48, or between 65 , 57, or between 70 , 97, have write this:

var keycode = // value; if(     keycode > 102 ||     keycode < 48 ||     (keycode > 57 && keycode < 65) ||     (keycode > 70 && keycode < 97) ) {     // } 

as can see, write keycode in if condition 6 times. if condition more complex (this 1 rules out non-hexadecimal characters), more repetitive.

i wondering if there way simplify this. maybe this:

if(     keycode > 102 || < 48 || (> 57 && < 65) || (> 70 && < 97) ) {     // } 

in omit variable name , put many conditionals on single variable. it's opposite of this question, in asker wondering if there way check multiple variables against single condition.

i'm coding in js now, if know implementations of in other language, might helpful in future well. thought there easy implementation of this, because use often, surprised see couldn't find information on language feature @ all.

the way simplify know create separate functions meaningful names, like:

function inrange(value, min, max) {     return (value > min) && (value < max); } function outrange(value, min, max) {     return (value < min) || (value > max); } 

then condition this:

if (outrange(keycode, 48, 102) || inrange(keycode, 57, 65) || inrange(keycode, 70, 97) ) {     ... } 

which little bit easier understand because see original intent, instead different > < , trying understand means

another approach creation of object rangechecker/notinrangechecker, create array of different range checkers , create function checkvalueatleastinonerange(keycode, rangesarray)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -