How to concatenate input value to function?

Asked

Viewed 214 times

1

Being a JS novice, I’m trying to concatenate the value of an input to a random value that I generated, as if typing the name generated a random user name for the person, I thought about saving the name to a function and then concatenating both, the problem is that I do not know how to make a function that saves the entered value. Someone willing to give a light?

Here are the functions I have so far, being function receber_nome() the one I can’t quite put together.

            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" );
        }   
  • You don’t need a function to take the name, just take the value of an input for example in which the person typed the name and concatenate with the generated value.

  • @Leandrade, you say using a Getelementbyid in the case?

  • This very thing Nathan.

3 answers

1


Nathan I’m going to post a very simple way for you to understand since you said you don’t 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>

  • I was going back and tinkering with this project and I would like to know how to insert a repetition looping in this gerar_serial function. For example, generate two keys. Got how? Thanks and sorry to bother again

  • Generate two keys where Nathan, for the same person?

  • This, coming out something like "Nathan - key: 0266; Nathan - key: 0148"

  • I edited the answer.

  • 1

    Thanks, I hadn’t thought of it. I was trying to put a go but never hit me hahaha

0

One of the things 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"
}));

  • 2

    I do not understand what is the relation of "programming in English".

  • 1

    The most important reason: English is the international language for code (and for documentation). More specifically, American English is the standard. I work in an Australian company and although Australia follows British grammar, the code we write follows American grammar (example: "color" and not "Colour"). So if that’s the world standard, why not follow it? If you plan to one day work outside of Brazil, potential companies wanting to hire you will want to see your code, but will not understand if they are in English.

  • Then try to leave your Github repositories with code and documentation in English. The commands and keywords of languages in programming are in English, so even if you wanted to write in Portuguese, it will inevitably end up being a mix. Some languages and frameworks are prepared to understand English in terms of semantics. Example: Ruby on Rails knows that the table in the database for a given model should always be the model name in the plural. That is,

  • when having a Person model, it will look for a people table. Contributions to open-source projects must be in English. I honestly do not know a famous open-source project that is not in English. Many companies in Brazil already write codes in English. So if you eventually start working at one of these companies, you’ll have to adapt anyway. Accents, "ç" and special characters do not work well in all languages, so you have misspelled words if you write them in English. You train and improve your English :)

  • "One of the things you have to keep in mind is getting used to programming in English, you understand." No. This will depend a lot on your project, whether you are developing a project for a specific Brazilian company, in which all your employees, developers and future developers will be Brazilian, and the code will not be open, has no reason to develop in English, the important thing is that the code is as clear as possible.

  • 1

    Your answer doesn’t answer your colleague’s question either, since what their question is how to get the value of the user name from the input, you simply sent a fixed value in the console.log

  • 2

    Dear Douglas, I understand the great intention about programming with knowing a little English, I very much agree with much of it, but the focus of the site is to answer the question problem, you posted the code, great, now post an explanation about the code, Your answer will probably be excellent (if correct of course), and about studying or not English would fit better as a comment (something you still can not do but soon will). So please edit the answer.

  • 1

    Complementing what @Guilhermenascimento, the fact that we have a Stack Overflow in Portuguese is precisely to meet the Portuguese speakers. If the person needed to rely on English (I know it helps, but the idea is not to depend on it) the original Stack Overflow would suffice. Our role here is just to provide content to make that bridge.

  • This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision

  • Dear @Celsomarigojr "responds yes", only that needs to be improved, I understand that is an automatically generated comment, but probably you voted in the analysis queue, I think it is not the case to convert into comment, you can tell by far that the code works, It’s just not explained. Reconsider analyzing a little bit more before voting in the analysis queue, this is the healthiest for the site.

  • 1

    I understand what you meant about learning to code in English, but as I said, I’m still a beginner in Javascript and to get the logic of it, Portuguese is more friendly to me, I intend to when more experienced to pass the codes of international standard. I know you gave the tip with the best intentions, so thank you very much. But I still intend to keep improving

  • 1

    @Guilhermenascimento "does not answer no", the focus of the question is how to make the logic of the function receber_nome() that as a basis should receive "the value of an input" according to the question. And the answer contains fixed values not showing how to get them dynamically from that input.

Show 7 more comments

-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))
})
  • The function is defined as gerar_serial and you’re calling as gerarSerial: Referenceerror.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.