JSFiddle - React, Tailwind, and code Playground

by dru_zod

JavaScript

var currentCodes = [];

var chars = '9aAbBcC0dDeEfF1gGhHiI2jJkKlL3mMnNoO4pPqQrR5sStTuU6vVwWxX7yYzZ8';

var getRandomInt = function (min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
};

var getCaseUniqueIdentifier = function () {

  var uniqueCode = [];
  for (var i = 0; i < 5; i += 1) {
  	uniqueCode.push(chars[getRandomInt(0, chars.length)]);
  }
  uniqueCode = uniqueCode.join('');
  
  var identifierAlreadyUsed = currentCodes.indexOf(uniqueCode) >= 0;

  if (identifierAlreadyUsed) {
  	var text = uniqueCode + ' is already defined (' + currentCodes.length + ' cases)';
  	console.log(text);
  	return getCaseUniqueIdentifier();
  }
    
  currentCodes.push(uniqueCode);
  
  return uniqueCode;
}

setInterval(function () {
	getCaseUniqueIdentifier();
}, 10);