I'm trying to generate an ID, but I do not want any duplicates (Obviously), so is there a way to loop a function until an unused ID is found?
Here's the current Code I'm trying to use:
Obviously there'll be more than just 10 IDs, but this is just for simplicity sake
let used_IDs = [1, 2, 3, 5, 6, 7, 8, 9, 10];
function randomID(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
let newID = randomID(1, 10);
if (used_IDs.includes(newID)) {
new ID = randomID(1, 10);
return console.log('Your new ID is ' + newID);
} else {
return console.log('Your new ID is ' + newID);
}
The problem with this code is that it'll only work once. How do I loop it? To make sure another duplicate doesn't occur?