Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

73
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!