Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

76
Visualizações
Javascript Large amount of conditions

I am building a quiz to determine what kind of skin you have based off 3 questions (oily, dry.. etc)

Is the best way to write the logic for this, something like the below or is there a more efficient way of doing this in terms of performance

if (q1 == 1 && q2 == 1 && q3 == 1){ skinType = "DRY" } else
if (q1 == 1 && q2 == 1 && q3 == 2){ skinType = "DRY" } else
if (q1 == 1 && q2 == 1 && q3 == 3){ skinType = "DRY" } else
if (q1 == 1 && q2 == 1 && q3 == 4){ skinType = "DRY" } else
if (q1 == 1 && q2 == 2 && q3 == 1){ skinType = "DRY" } else
if (q1 == 1 && q2 == 2 && q3 == 2){ skinType = "DRY" } else
if (q1 == 1 && q2 == 2 && q3 == 3){ skinType = "DRY" } else
if (q1 == 1 && q2 == 3 && q3 == 1){ skinType = "DRY" } else
if (q1 == 1 && q2 == 3 && q3 == 2){ skinType = "DRY" } else
if (q1 == 1 && q2 == 4 && q3 == 1){ skinType = "DRY" } else
if (q1 == 1 && q2 == 4 && q3 == 2){ skinType = "DRY" } else
if (q1 == 1 && q2 == 2 && q3 == 4){ skinType = "COMBO" } else
if (q1 == 1 && q2 == 3 && q3 == 3){ skinType = "COMBO" } else
    ....etc
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In terms of performace there's nothing faster than a simple condition, but not in terms of readability and code maintainance, which are also important in coding.

about 4 years ago · Juan Pablo Isaza Relatório

0

It depends on your data. Try to find patterns or input combinations that determine exactly the target class (e.g. q1 ==1 && q2 == 1 is always "DRY") and eleminate the redundant lines.

Since many of your input combinations seem to result in the same class, i.e., DRY, COMBO, you could also try to build nested conditions similar to a decision tree.

if (q1 == 1) {
    if (q2 == 1) {
        skinType = "DRY";
    } else if (q2 == 2) {
        if (q3 == 4) {
            skinType = "COMBO";
        } else {
            skinType = "DRY";
        }
    }

    //...
}

One more sophisticated option to build good decision trees is the ID3 algorithm.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Switch case statement for better performance("switch statement is faster in most cases when compared to if-else ").

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda