How to concatenate input value to function?

1

As a beginner in JS I'm trying to concatenate the value of an input to a random value that I generated, as if typing the name was generated a random user name for the person, I thought of saving the name in a function and then concatenating both , the problem is that I do not know how to do a function that saves the value typed. Someone willing to give a light?

Here are the functions I have so far, being function receber_nome() to which I can not mount logic.

            function receber_nome()
        {

        }

        function gerar_string_aleatoria( tam, charset )
        {
            var serial = "";

            for( var i = 0; i < tam; i++ )
                serial += charset.charAt( Math.floor(Math.random() * charset.length) );

            return serial;
        }


        function gerar_serial()
        {
            return receber_nome() + gerar_string_aleatoria( 4, "0123456789" );
        }   
    
asked by anonymous 31.07.2018 / 21:21

3 answers

1

Nathan will post a very simple way to understand you since you said that you do not know much about Javascript , because the answers given may confuse you a little.

function gerar_string_aleatoria( tam, charset ){
   var serial = "";

   for( var i = 0; i < tam; i++ ){
       serial += charset.charAt( Math.floor(Math.random() * charset.length));   
    }
    return serial; 
}

function gerar_serial(){
  var nome = document.getElementById('nome').value; // pega o valor do input
  var p = document.getElementById('p'); // pega a tag p
  
  p.innerText = nome+ " - chave: "+ gerar_string_aleatoria( 4, "0123456789")+"\n"+ nome+" - chave: "+ gerar_string_aleatoria( 4, "0123456789"); 
  // escreve o resultado dentro da tag p
}
<input id="nome" type="text" onblur="gerar_serial()">
<p id="p"></p>
    
31.07.2018 / 22:00
0

One thing you have to keep in mind is getting used to programming in English, you know.

const serialGenetator = (parameters) => {
    let { name: n, size: t, characters: c } = parameters, s = "";
    for(let i = 0; i < t; i++) 
      s += c.charAt(Math.floor(Math.random() * c.length));
    return n + s;
}

console.log(serialGenetator({
    name: "Douglas", //document.querySelector("element").value
    size: 10,
    characters: "0123456789"
}));
    
31.07.2018 / 21:52
-1
const input = document.getElementById("seu-input");

function gerar_string_aleatoria( tam, charset ) {
  var serial = "";

  for( var i = 0; i < tam; i++ )
    serial += charset.charAt( Math.floor(Math.random() * charset.length) );

  return serial;
}


function gerar_serial(nome) {
  return nome + gerar_string_aleatoria( 4, "0123456789" );
}   

input.addEventListener('keydown', function(e) {
    if(e.keyCode !== 13) return;
    console.log(gerar_serial(e.target.value))
})
    
31.07.2018 / 21:44