I'm trying to output all the possible combinations of 4 character strings using uppercase letters.
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNPRSTUVWXYZ';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function getcodes(){
var newcode =makeid(4);
if (codes.includes(newcode)){
getcodes();
}
else
{
codes.push(newcode)
document.getElementById("demo").innerHTML += newcode + "<br>";
getcodes();
}
}
getcodes()
console.log(codes.length)
The problem, of course, is that this loops forever. I'm not sure how to stop the loop when all the codes have been generated. How can I stop this after all combinations have been created?
The use case is that we're trying to generate unique URLs that will be something like example.com/CODES-HERE The codes generated with this script will be used for the CODES-HERE part.