You can use a switch
statement.
Wrap it up in a function together with the round
logic:
function getTax( price ){
var taxRate = 0;
switch( true ){
case ( price < 2 ):
// or case ( price <= 1 ) depending on what you need
break;
case ( price < 6 ):
taxRate = 0.05;
break;
case ( price < 11 ):
taxRate = 0.1;
break;
case ( price < 16 ):
taxRate = 0.15;
break;
default:
taxRate = 0.2;
}
return Math.round( price * taxRate * 100 ) / 100;
}
And then call the function to get the returned value:
var tax = getTax( price );
switch( true )
can be a confusing usage of this statement and not everyone like it. You can read about it here and here. Go for if
statements if you prefer it.
Also, beware of some caveats regarding the Math.round
function.