vumeter % 50 < vumeter
doesn't make much sense to me. For non-negative numbers, this is equivalent to vumeter > 50
, so you can just use that instead. For negative numbers, this will always return false
, although it seems like this would be an illegal argument to your function.
@mleyfman provided a great solution. In practice, the code provided achieves something called clamping, where you restrict a value to a specified range. You can roll your own clamp
function like so:
clamp = (value, min, max) -> Math.min(Math.max(min, value), max)
levels = (vumeter) ->
'green' : clamp(vumeter, 0, 50)
'yellow' : clamp(vumeter, 50, 85) - 50
'red' : clamp(vumeter, 85, 100) - 85
// or, if you find it more meaningful to provide maxima as parameters rather than ranges:
'green': clamp(vumeter, 0, 50)
'yellow': clamp(vumeter - 50, 0, 35)
'red': clamp(vumeter - 85, 0, 15)