dimanche 23 janvier 2022

Random number generator producing "undefined" result [duplicate]

My random number generator produces mostly what I want, however approximately 1/10 times it produces an "undefined" result. Here's my code:

    function generateID() {
        var generatedID = Math.floor(Math.random() * 99999999);
        var idTaken = false;
        userData.forEach(task => {
            if (task.id == generatedID) {
                idTaken = true;
            }
        })
        if (idTaken || generatedID < 10000000) {
            generateID();
        } else {
            return generatedID;
        }
    }
    
    console.log(generateID());



Aucun commentaire:

Enregistrer un commentaire