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

228
Views
¿Cómo puedo limpiar esta función if-else? (Javascript ES6)

Soy un desarrollador principiante de React.

Estoy limpiando mis códigos.

Estoy tratando de deshacerme de la declaración if-else tanto como sea posible, pero no sé cómo manejar esta función.

 const calc = () => { if (100 < responsiveWidth.phone) { setPerPage(1); } else if (100 < responsiveWidth.tablet) { setPerPage(2); } else if (100 < responsiveWidth.smallDesktop) { setPerPage(3); } else if (100 < responsiveWidth.desktop) { setPerPage(4); } else { setPerPage(6); } };

Realmente odio este código. ¿Me puede ayudar?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede encontrar el índice ( Array#findIndex ) y agregar uno.

 const widths = [width.phone, width.tablet, width.smallDesktop, width.desktop], calc = width => setPerPage(widths.findIndex(w => width < w) + 1) || 6);
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!