I need to edit this javascript code to generate letters and numbers

-3

The code is this below I need in it generate letters and numbers instead of just numbers how do I?

<html>
<head>
<title>Gerador de CPF e CNPJ V&aacute;lidos</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
&lt;<script language="JavaScript">

// ****************************************
// Script Gerador de CPF e CNPJ Válidos
// Autor: Marcos Guiga
// Site : Worldigital.co.cc
// Email: [email protected]
// Data:  19/12/2010
// ****************************************
// Função para gerar números randômicos
function gera_random(n)
{
    var ranNum = Math.round(Math.random()*n);
    return ranNum;
}

// Função para retornar o resto da divisao entre números (mod)
function mod(dividendo,divisor)
{
          return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}

// Função que gera números de CPF válidos
function cpf()
{
           var n = 9;
           var n1 = gera_random(n);
           var n2 = gera_random(n);
           var n3 = gera_random(n);
           var n4 = gera_random(n);
           var n5 = gera_random(n);
           var n6 = gera_random(n);
           var n7 = gera_random(n);
           var n8 = gera_random(n);
           var n9 = gera_random(n);
           var n10 = gera_random(n);
           var n11 = gera_random(n);
           var n12 = gera_random(n);
           var d1 = n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
           d1 = 11 - ( mod(d1,11) );
           if (d1>=10) d1 = 0;
           var d2 = d1*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
           d2 = 11 - ( mod(d2,11) );
           if (d2>=10) d2 = 0;
           return ''+n1+n2+n3+n4+'-'+n5+n6+n7+n8+'-'+n9+n10+n11+n12+'-'+d1+d2+d1+d2;
}

// Função que gera números de CNPJ válidos
function cnpj()
{
          var n = 9;
          var n1  = gera_random(n);
           var n2  = gera_random(n);
           var n3  = gera_random(n);
           var n4  = gera_random(n);
           var n5  = gera_random(n);
           var n6  = gera_random(n);
           var n7  = gera_random(n);
           var n8  = gera_random(n);
           var n9  = 0;//gera_random(n);
           var n10 = 0;//gera_random(n);
           var n11 = 0;//gera_random(n);
           var n12 = 1;//gera_random(n);
          var d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
           d1 = 11 - ( mod(d1,11) );
           if (d1>=10) d1 = 0;
           var d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
           d2 = 11 - ( mod(d2,11) );
           if (d2>=10) d2 = 0;
           return ''+n1+n2+'.'+n3+n4+n5+'.'+n6+n7+n8+'/'+n9+n10+n11+n12+'-'+d1+d2;
}

// Função para escolher qual função chamar de acordo com a chamada
function faz()
{
          if (document.form1.tipo[0].checked)
                    document.form1.numero.value = cpf();
          else
                    document.form1.numero.value = cnpj();
}

// FIM
</script>
</head>

<body>
<form name="form1" method="post" action="">
  <table width="261" border="0" align="center" cellpadding="0" cellspacing="2">
    <tr align="center">
      <td colspan="2" bgcolor="#003399"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Gerador
        de CPF e CNPJ Válidos</strong></font></td>

    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#CCCCCC"><input name="numero" type="text" id="numero" size="20"></td>
    </tr>
    <tr>
      <td width="168" align="center" bgcolor="#CCCCCC"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
        <input name="tipo" type="radio" value="cpf" checked>
        CPF
        <input type="radio" name="tipo" value="cnpj">
        CNPJ</font></td>

      <td width="126" align="center" bgcolor="#CCCCCC"><input type="button" name="Button" value="Gerar" onClick="faz()"></td>
    </tr>
  </table>
</form>
<div align="center">
     <font size="1" face="Verdana, Arial, Helvetica, sans-serif">© 2010 - 2011 por: Marcos Guiga
      <br> 
</font> </div>
</body>
</html>
    
asked by anonymous 29.09.2018 / 12:25

1 answer

0

I would create an array with all lowercase letters of the alphabet.

var alfabeto = ['a','b','c','d','e','f',...,'X','Y','Z'];

And you would still use your code only with the corresponding numbers in that vector. For example, your code generated the numbers 10,33,13,5.

Making an equivalence to that vector would be jjme.

On the other hand if what you want are the two together, you would do it the same way, however adding the numbers to the vector as well.

So:

var num_e_letra = ['0','1','2','3','4','5','6','7',...,'X','Y','Z']

For this, random is done using the maximum length of the vector.

Edit

I just made the code below, in it I created a function that generates a sequence of random numbers and letters, the other generates a random letter.

random.html

<head>

</head>
<body>
    <p>KKK</p>
    <br>
    <button onclick="seqNumLetra()">Clique Aqui!</button>
    &nbsp;&nbsp;
    <button onclick="seqLetra()">Clique Aqui Depois!</button>
    <p>Teste: </p> <p id="result">Resultado</p>
    <br>
    <p>Teste2: </p> <p id="result2">Resultado</p>
    <script>
            var letra_num = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
            var letra = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
            // minimo: 0
            // maximo: 33

            function genNumAndLetter(cont) {
                let randomNum = Math.round(Math.random() * cont);
                return randomNum
            }

            function seqNumLetra() {
                let a = letra_num[genNumAndLetter(letra_num.length)]; console.log("a: ",a);
                let b = letra_num[genNumAndLetter(letra_num.length)]; console.log("b: ",b);
                let c = letra_num[genNumAndLetter(letra_num.length)]; console.log("c: ",c);
                let d = letra_num[genNumAndLetter(letra_num.length)]; console.log("d: ",d);
                let e = letra_num[genNumAndLetter(letra_num.length)]; console.log("e: ",e);

                let seq = a.concat(b,c,d,e); console.log("seq: ", seq);

                document.getElementById('result').innerHTML = seq;
            }

             function seqLetra() {
                let a = letra[genNumAndLetter(letra.length)];
                let b = letra[genNumAndLetter(letra.length)];
                let c = letra[genNumAndLetter(letra.length)];

                let seq = a.concat(b,c);

                document.getElementById('result2').innerHTML = seq;
            }


    </script>
</body>
    
29.09.2018 / 12:38