I really hate using if’s inside my app and I thought this is a pretty straightforward pattern so is there any way to turn the below code into a 1 line formula just for better readability inside my app?
This is the code:
int duration = 0;
if (score < 100) {
duration = 2;
} else if (score >= 100 && score < 200) {
duration = 3;
} else if (score >= 200 && score < 300) {
duration = 4;
} else if (score >= 300 && score < 400) {
duration = 5;
} else if (score >= 400 && score < 500) {
duration = 6;
} else if (score >= 500) {
duration = 7;
}
I myself am not that good at math or with coming up with formulas for that matter so would anybody be able to help me get a formula to achieve what the code above does?
Thanks!
Looks like we can rewrite this as follows:
Am I missing anything? :-/
EDIT if score might be negative, we have to add one more cap:
EDIT 2 to symmetrically handle negatives, you can use the following: