Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

72
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda