algorithm - What is the most compact way in JavaScript to force a value in a range of values? -


this question has answer here:

i have simple 2 line procedure that's like

if ( bgwidth < 0 ) { bgwidth = 0; } else if ( bgwidth > 100 ) { bgwidth = 100; } 

and i'm wondering compact way it. there way bitshift operators? :)

math.min() , math.max() might trick:

math.min(math.max(0, bgwidth), 100); 

and readbility:

function fitbetween(value, min, max) {   return math.min(math.max(min, value), max); } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

post - imageshack API cURL -