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
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.
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.
You can use Switch case statement for better performance("switch statement is faster in most cases when compared to if-else ").