Simplify a Javascript Switch

1

I have the following code:

switch (az) {
  case 0:
        var azul = "Nivel de Riesgo: [0] Sin Riesgo";
        break;
  case 1:
        var azul = "Nivel de Riesgo: [1] Poco Peligroso";
        break;
  case 2: 
        var azul = "Nivel de Riesgo: [2] Peligroso";
        break;
  case 3:
        var azul = "Nivel de Riesgo: [3] Muy Peligroso";
        break;
case 4:
        var azul = "Nivel de Riesgo: [4] Mortal";
        break;
}

switch (ro) {
  case 0:
        var rojo = "Inflamabilidad: [0] No se Inflama";
        break;
  case 1:
        var rojo = "Inflamabilidad: [1] Inflama a mas de 93°C";
        break;
  case 2: 
        var rojo = "Inflamabilidad: [2] Inflama a partir de 93°C";
        break;
  case 3:
        var rojo = "Inflamabilidad: [3] Inflama a parir de 37°C";
        break;
case 4:
        var rojo = "Inflamabilidad: [4] Inflama a parir de 25°C";
        break;
}

the values of az and ro logically go from 0 to 4 but they can take different values example az = 0 ro = 3 and I require that in the blue and red output corresponds to the case of each one, my query is can be joined these 2 or 3 switch since I also have the switch am that gives me the case from 0 to 4 for the yellow variable. Just place 2 of them to see if you can to implement it. Thanks.

    
asked by Jose M Herrera V 21.03.2018 в 03:46
source

3 answers

4

You can create an array with the values you need to compare. For example if az = 2 you need to traverse the array to find the match, that is, if it equals a value within the array.

var valores = [0,1,2,3,4]; //puedes reutilizarlo con az, ro y am.
var riesgos = ["sin riesgo","poco peligroso","muy peligroso","mortal"];
var az = 2; //por ejemplo
var azul = "";

//utiliza un foreach
valores.forEach(function(elemento) {
    if(az == elemento) 
    azul = "Nivel de Riesgo: ["+elemento+"] "+riesgos[elemento];
});

console.log(azul);
    
answered by 21.03.2018 / 04:40
source
0

//podrias hacer un arreglo por cada color riegoRojo,  riesgo Azul etc..
var riesgosAzul = ["Sin Riesgo", "Poco Peligroso", " Peligroso", "Muy peligroso", "Mortal"];
var riesgoRojo = ["No se Inflama", "Inflama a mas de 93°C", "Inflama a partir de 93°C", "Inflama a parir de 37°C", "Inflama a parir de 25°C"];
var az = 0;
var ro = 0;
/*  mensaje : es el mensaje para ese determinado color, 
    opcion : la opcion que eligio el usuario
    riesgos : el array de los riesgos
*/
function obtenerObservacion(mensaje, opcion, riesgos) {
  return mensaje + " [" + opcion + "] " + riesgos[opcion];
}

var azul = obtenerObservacion("Nivel de  riesgo :", az, riesgosAzul);
var rojo = obtenerObservacion("Inflamabilidad :", ro, riesgoRojo);

console.log(azul);
console.log(rojo);
    
answered by 21.03.2018 в 07:22
0

You can create an object where you have the keys (level) associated with the answers and through a function in this case nivel the string is returned with the final response already filtered from the object

var riesgos = {  // Objeto donde estan las opciones dependiendo del nivel o llave [0-4].
 0: "Sin Riesgo ",
 1: "Poco Peligroso",
 2: "Peligroso",
 3: "Muy Peligroso",
 4: "Mortal"
};


var nivel = (az) => {
 return 'Nivel de Riesgo: [${az}] ${riesgos[az]}' 
 // aquí retornas el string final donde riesgos es el objeto al cual le vas a realizar la búsqueda, y az el valor de la llave a buscar.
}

console.log(nivel(2))

If you do not want Arrow Function or text string templates you can also add the function like this:

var nivel = function nivel(az) {
  return "Nivel de Riesgo: [" + az + "] " + riesgos[az];
  // aquí retornas el string final donde riesgos es el objeto al cual le vas a realizar la búsqueda, y az el valor de la llave a buscar.
};
    
answered by 22.03.2018 в 15:22