J'ai ce code et je veux générer un makeid quel que soit le nombre entré

var rl = require("readline");
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("How many codes would you like to generate? ", function (number) {
    function makeid(length) {
        var result = '';
        var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var charactersLength = characters.length;
        for (var i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }
});
-2
InfinityGaming 14 janv. 2020 à 03:10

1 réponse

Meilleure réponse

Aussi simple que ça?

const CODE_LENGTH=16; // or whatever - get it from somewhere...
var rl = require("readline");
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("How many codes would you like to generate? ", function (number) {
    function makeid(length) {
        var result = '';
        var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var charactersLength = characters.length;
        for (var i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }
    for(var i=0; i<number; i++) {
      var code = makeid(CODE_LENGTH);
      console.log(code); // do whatever with the code...
    }
});
1
see sharper 14 janv. 2020 à 00:29