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

95
Views
intersection between two ranges

I have an array of goods with price range "from, to"

 const goods = [
        { name: "Sweets", prices: [0, 100] }, 
        { name: "Chocolate", prices: [500, null] }, 
        { name: "Cheesecake", prices: [100, 200] }, 
        { name: "Truffle", prices: [null, null] },
        { name: "Apple cake", prices: [null, 400] },
        { name: "Banana cake", prices: [50, 250] },
        { name: "Raspberry cake", prices: [200, null] },
        { name: "Donuts", prices: [51, 450] },
        ,
    ];

to filter them with required options where null in tuple [null, 200] means max price of product is 200 and [200, null] means min price of product is 200.

    const requiredRange1 = [null, 200];
    const requiredRange2 = [100, 350];
    const requiredRange3 = [200, null];
    const requiredRange4 = [200, 300];
    const requiredRange5 = [1000, null];

for example: output of requiredRange1 is

[
  { name: "Sweets", prices: [0, 100] },
  { name: "Cheesecake", prices: [100, 200] },
  { name: "Apple cake", prices: [null, 400] },
  { name: "Banana cake", prices: [50, 250] },
  { name: "Raspberry cake", prices: [200, null] },
  { name: "Donuts", prices: [51, 450] },
]

I wrote a function

const filterCourses = (arrayOfCourses, requiredRange) => {

  const [minRequiredPrice, maxRequiredPrice] = requiredRange;

  const filterCallback = (course) => {
    const maxCoursePrice = course.prices[1];
    const minCoursePrice = course.prices[0];
    
    if(minCoursePrice === null && maxCoursePrice === null) {
      return false
    }
    
      if(minRequiredPrice === null) {
        return maxRequiredPrice >= minCoursePrice
      } else if(maxRequiredPrice === null) {
        return minRequiredPrice <= maxCoursePrice || minCoursePrice >= minRequiredPrice
      } else {
        return (minRequiredPrice > maxCoursePrice && maxCoursePrice !== null) || (maxRequiredPrice < minCoursePrice && minCoursePrice !== null) ? false : true
  } 
  }
  const result = arrayOfCourses.filter(filterCallback)

  return result
}

Looks pretty hard-coded, how can I filter this array with more elegant way?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

More elegant:

function doRangesIntersect(a, b) {
  const lo = Math.max(a[0] ?? -Infinity, b[0] ?? -Infinity);
  const hi = Math.min(a[1] ??  Infinity, b[1] ??  Infinity);
  return lo <= hi;
}

function filterCourses(arrayOfCourses, requiredRange) {
  return arrayOfCourses.filter(course => doRangesIntersect(course.prices, requiredRange));
}
about 4 years ago · Santiago Gelvez 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!