How to create a list of numbers (in CSV format) based on a set of criteria

Scenario: Each user on an app can buy event tickets that are numbered 1 to 1000 BUT they cannot buy a ticket that is within 10 places of any tickets they’ve already bought.
So if the first ticket they bought is ticket number 30 then they cannot buy any tickets between 20 and 40 (ie. they cannot buy tickets with the number 20,21,22,23,24,25…38,39,40)

NB: if they buy ticket 30, 45, 82 then the list of ticket numbers they cannot buy next are (20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,54,55 as well as 72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92)
if the result could be listed as comma separated values then i could use it to hide buttons that they aren’t allowed to buy.
Any ideas how this can be done. Is it possible with Query column?

I think you can use JavaScript for this.

function getInvalidTickets(purchasedTickets) {

  if (typeof purchasedTickets === 'number') {
    purchasedTickets = [purchasedTickets];
  } else {
    purchasedTickets = purchasedTickets.split(',').map(Number);
  }

  const ranges = purchasedTickets.map(number => {
    const min = Math.max(number - 10, 1);
    const max = Math.min(number + 10, 1000);
    return {min, max}; 
  });

  const invalidTickets = [];

  ranges.forEach(range => {
    for (let i = range.min; i <= range.max; i++) {
      if (purchasedTickets.some(num => Math.abs(num - i) <= 10)) {
        invalidTickets.push(i);
      }
    }
  });

  return invalidTickets.join(',');
}

return getInvalidTickets(p1)

1 Like

Wow, thanks for that, it looks perfect. I’m going to test it out right now. Thanks a million!

Thanks @ThinhDinh it works perfectly👌

1 Like

Great to hear!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.