切换案例到/之间

2022-01-19 00:00:00 switch-statement javascript

在 Javascript 中有没有一种方法可以在不使用 if 语句的情况下通过 switch case 结构将一个整数与另一个整数进行比较?

Is there a way in Javascript to compare one integer with another through switch case structures without using if statements?

例如

switch(integer) {
    case 1 to 10:
        break;
    case 11 to 20:
        break;
    case 21 to 30:
        break;
}

推荐答案

你可以做一些数学运算.

You can do some math manipulations.

switch(Math.ceil(integer/10)) {
    case 1: // Integer is between 1-10
        break;
    case 2: // Integer is between 11-20
        break;
    case 3: // Integer is between 21-30
        break;
}

相关文章