IF ELSE Giant

-2

Hello, I would like help from the university ... rs

I would like to know if I'm doing it right or if there is a better alternative to simplify this lot of if else .

//APLICAÇÃO: [SEG] Sobre seguro, [KML] KmLivre, [DIA] Diária, [IE] Impostos/Encargos, [VLT] no valor total
//VALOR EM.: [MOT] Montante, [POR] Porcentagem.
//CALCULO..: [DIA]Por dia, [FIX]Fixo
//TIPO.....: [TXV]Taxa de Venda, [DES]Desconto, [ENC]Encargos Extras


if (aplicacao == "SEG") {

} else if (aplicacao == "KML") {
  if (valor_em == "POR") {
    if (calculo == "PD") {
      if (tipo == "TXV") {

      } else if (tipo == "DES") {

      } else if (tipo == "ENC") {

      }
    } else {
      if (tipo == "TXV") {

      } else if (tipo == "DES") {

      } else if (tipo == "ENC") {

      }
    }
  } else {

  }
} else if (aplicacao == "DIA") {

} else if (aplicacao == "IE") {

} else if (aplicacao == "VLT") {

}

Is this path even if I have to follow or is there something better?

    
asked by anonymous 25.11.2018 / 19:04

1 answer

-3

If it's the way I'm thinking, where the condition has to be for a given string value, I'd try that way using switch case:

     let arr = ['seg', 'ter', 'qua'];
     var cond = arr.findIndex( aplicacao => aplicacao === 'seg');
     //ou
     var cond = arr.findIndex( aplicacao => aplicacao === 'ter');
     //ou
     var cond = arr.findIndex( aplicacao => aplicacao === 'qua');

     switch(cond){
       case 0:
        console.log('executa instrucao pra seg')
        break;
       case 1:
        console.log('executa instrucao pra ter')
        break;
       case 2:
        console.log('executa instrucao pra qua')
        break;
       default:
        console.log('eu faria assim pra tentar diminuir o monte de IF ELSE');
     }
    
26.11.2018 / 08:49