Question: 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!

Answer: 
Looks like we can rewrite this as follows:

```
int duration = MIN(2 + (score / 100), 7);

```

Am I missing anything? :-/

**EDIT** if score might be negative, we have to add one more cap:

```
int duration = MAX(MIN(2 + (score / 100), 7), 2);

```

**EDIT 2** to symmetrically handle negatives, you can use the following:

```
int duration = MIN(2 + (abs(score) / 100), 7);

```
It's something close to (aka this is untested):

```
-(int) duration:(int) score {
    score = (score > 500) ? 500 : score; // over 600 == 600
    return (score+100)/100 + 1
}

```