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

147
Views
how to reduce tertiary conditionals

So I have this code which works perfectly, it's just that it looks ugly and takes up alot of space. is there a way to reduce the amount of lines this code takes? Code here:

radius:
      Platform.OS === 'android'
        ? showMore === 1
          ? androidRadius * 2
          : showMore === 2
          ? androidRadius * 4
          : showMore === 3
          ? androidRadius * 10
          : showMore === 4
          ? androidRadius * 25
          : showMore === 5
          ? androidRadius * 50
          : showMore === 6
          ? androidRadius * 100
          : androidRadius
        : showMore === 1
        ? iosRadius * 2
        : showMore === 2
        ? iosRadius * 4
        : showMore === 3
        ? iosRadius * 10
        : showMore === 4
        ? iosRadius * 25
        : showMore === 5
        ? iosRadius * 50
        : showMore === 6
        ? iosRadius * 100
        : iosRadius,
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's quite easy to clean this kind of condition using mapping, for instance:

const radiusMap = {1: 2, 2: 4, 3: 10, 4: 25, 5: 50, 6: 100};
const platformRadius = Platform.OS === 'android' ? androidRadius: iosRadius;

// finally,
return { radius: radiusMap[platformRadius] || platformRadius };

This will take the radius from the radius map, also returning a default radius if no radius in the map is found.

about 4 years ago · Juan Pablo Isaza Report

0

I usually use object lookups for these situations. So, for showMore cases, you can do this: -

function getRadius(deviceRadius, showMoreValue){
    const showMoreCases = {
        1: deviceRadius * 2,
        2: deviceRadius * 4,
        3: deviceRadius * 10,
        4: deviceRadius * 25,
        5: deviceRadius * 50,
        6: deviceRadius * 100,
    }

    return showMoreCases[showMoreValue] || deviceRadius;
}

And you can use it in the first condition like this: -

radius: Platform.OS === 'android'
        ? getRadius(androidRadius, showMore) 
        : getRadius(iosRadius, showMore)
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!