Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:47:25+00:00 2026-06-14T01:47:25+00:00

I really hate using if’s inside my app and I thought this is a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:47:26+00:00Added an answer on June 14, 2026 at 1:47 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hate using SO as a way to find simple functions, but I really
I really hate this error, because it can be so hard to pin point.
I don't see how this is possible, but I really, really hate to run
I really hate to beat this dead horse, but I couldn't find a clear
I'm using Rake to generate RDoc documentation for my project, but I really hate
I really hate to ask this question, because it seems like it's been asked
I really hate the way my shopping cart is set up as I CANNOT
I've been using south but I really hate having to manually migrate data all
Something which I really hate is to cast each element or attribute value from
Really confused why the QR output using RcppArmadillo is different than QR output from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.