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

117
Views
Best way to handle value tied to conditional

In a function I have I try to take a value, and if the value ends up as undefined, then I call the same function but with a different parameter

let price = await getPrice(name, category, '1');

if (price === undefined) {
   price = await getPrice(name, category, '2');

   if (price === undefined) {
      price = await getPrice(name, category, '3');
   }
}

I'm told that this approach can be very error prone, and after a bit of analyzing, I can see why. However, I don't know if there's a better way to handle this.

I need the third parameter since I use it to process a URL, as that URL has one of those strings.

static getPrice(productName, productCategory, variantNumber) {
 ...
 let url = `https://exampleurl.com/${productName}cat=${productCategory}${variantNumber}`;
 ...
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use a loop and stop when you get a defined result.

const variants = ['1', '2', '3'];
let price;
for (let i = 0; i < variants.length; i++) {
  price = await getPrice(name, category, variants[i]);
  if (price !== undefined) {
    break;
  }
}
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!