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

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -