JSFiddle - React, Tailwind, and code Playground

EE_UK, increment ban and get next index if it is already in use

by Yurii Predborskyi

JavaScript

let BAN_INDEX_SEPARATOR = '_';
let invoice = 'abba123';
let bansInUse = ['1', '2', '4'];
let ban = '1';

function banInUse(ban) {
	return bansInUse.includes(ban);
}

function mapBan(ban) {
  if (!banInUse(ban)) {
  	bansInUse.push(ban);
    console.log('mapped ban ' + ban);
  } else {
    mapBan(getNextBan(ban));
  }
}

function getNextBan(ban) {
  let index = ban.indexOf(BAN_INDEX_SEPARATOR);
  if (index === -1) {
    return ban + BAN_INDEX_SEPARATOR + 1;
  } else {
    return ban.substring(0, index) + BAN_INDEX_SEPARATOR + (+ban.substring(index + 1) + 1);
  }
}

console.log('before');
console.log(bansInUse);
console.log('after');
mapBan(ban);
console.log(bansInUse);