¿Cómo puedo convertir esta declaración anidada en algo más legible y 'elegante'?

if(speed==0){
            othvalue=0;
        }else {
            if(speed>value1864cmn){
                othvalue=value1864cmn;
            }else {
                if(speed>value1746cmn){
                    othvalue=value1746cmn;
                }else {
                    if(speed>value1628cmn){
                        othvalue=value1628cmn;
                    }else {
                        if(speed>value1510cmn){
                            othvalue=value1510cmn;
                        }else {
                            if(speed>value1392cmn){
                                othvalue=value1392cmn;
                            }else {
                                if(speed>value1274cmn){
                                    othvalue=value1274cmn;
                                }else {
                                    if(speed>value1156cmn){
                                        othvalue=value1156cmn;
                                    }else {
                                        if(speed>value1038cmn){
                                            othvalue=value1038cmn;
                                        }else {
                                            if(speed>value920cmn){
                                                othvalue=value920cmn;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
1
gerpaick 18 jun. 2011 a las 06:47

5 respuestas

La mejor respuesta

Puedes usar la construcción "match":

var othvalue = (speed == 0) ? 0 
             : (speed <= 10) ? 10
             : (speed <= 20) ? 20
             : (speed <= 30) ? 30
             : 40;
5
c-smile 18 jun. 2011 a las 04:17
if (speed == 0) {
    othvalue = 0;
} else {
    var values = [
        value1746cmn, value1628cmn, value1510cmn, value1392cmn, value1274cmn,
        value1156cmn, value1038cmn, value920cmn
    ];
    for (var i in values) {
        if (speed > values[i]) {
            othvalue = values[i];
            break;
        }
    }
}
1
rid 18 jun. 2011 a las 02:57
if (speed == 0) {
    othvalue = 0;
} else if (speed > value1864cmn) {
    othvalue = value1864cmn;
} else if (speed > value1746cmn) {
    othvalue = value1746cmn;
} else if (speed > value1628cmn) {
    othvalue = value1628cmn;
} else if (speed > value1510cmn) {
    othvalue = value1510cmn;
} else if (speed > value1392cmn) {
    othvalue = value1392cmn;
} else if (speed > value1274cmn) {
    othvalue = value1274cmn;
} else if (speed > value1156cmn) {
    othvalue = value1156cmn;
} else if (speed > value1038cmn) {
    othvalue = value1038cmn;
} else if (speed > value920cmn) {
    othvalue = value920cmn;
};
1
Igor 18 jun. 2011 a las 02:49

No necesita anidar esto, ni convertirlo en una declaración de cambio. Simplemente coloque if justo al lado de else.

Por ejemplo:

if(speed==0){
    othvalue=0;
} else if(speed>value1864cmn){
    othvalue=value1864cmn;
} else if {
...
3
Andrew Arnott 18 jun. 2011 a las 02:49

// No sé sobre elegante, pero recorrer una matriz es bastante rápido.

function setSpeedValue(speed){
    var L= 9, values= [0, 920, 1038, 1156, 1274, 1392, 1510, 1628, 1746, 1864];
    while(values[L]>speed)--L;
    return 'value'+values[L]+'cmn';
}
alert(setSpeedValue(1040))
1
kennebec 18 jun. 2011 a las 03:09