How to gracefully judge the range of a number

The

question is as follows: pass in a value and how to elegantly judge the interval.
clipboard.png

except for using if.else.

is there any better choice?

Mar.14,2021


although using if-else is not elegant, it can achieve the highest efficiency. If you have to be elegant and there is no rule in the area you want to divide, you can only cycle through the whole area from beginning to end, just like the one upstairs.


give me an idea

function getRange(range,score){
    for(const r of range){
       ... 
   }
}
var range=[0,0.45,0.75,0.9]
getRange(range,0.5);

level = [0.45, 0.75, 0.9, 1.0]
def get_level(point):
    ret = 0
    while point > level[ret]:
        ret += 1
    return ret
Menu