Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Why Random variable is only returning 1 character

Hey guys im trying to generate a random password using upper and lowercase keys + numbers. the code seems to work but instead of returning 20 characters its instead returning only 1. The return seems random. The element should be replaced why the random password every time the button is clicked.

HTML

<button id = "button5" onclick = "password()">Generate password </button> 
<p4 id = "p4" > Your password will apear here </p4>
 

This is the Javascript

function password (length ) {
var ranpassword = "";
var chara = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"


var charalength = chara.length // there is an outside variable defining charalength = ""; I could not include that here 
for (var i = 0; <length ; i++) {
ranpassword +=  chara.charAt(Math.floor(Math.random() *
chara.length));
return ranpassword;

}
document.getElementById("p4").innerHTML = "hello there " + ranpassword;
console.log(password(20));

}

whenver the button is clicked, one random letter is returned in console.log and i cant seem to understand why ? Can anyone tell me why? Any help would be great . Thanks :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You did some mistake. Try this code,

function password (length) {
    let ranpassword = "",
        chara = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ",
        charalength = chara.length;

    for (var i = 0; i<length ; i++) {
        ranpassword +=  chara.charAt(Math.floor(Math.random() *
        chara.length));
    }

    document.getElementById("p4").innerHTML = "hello there " + ranpassword;
}
<button id = "button5" onclick="password(20)">Generate password </button> 
<p4 id = "p4" > Your password will apear here </p4>

about 4 years ago · Juan Pablo Isaza Relatório

0

Aside from your password function implementation partly wrong, you are not passing a length to password() function in your onclick handler.
You need to pass the length as well like password(20):

<button id = "button5" onclick = "password(20)">Generate password </button> 
about 4 years ago · Juan Pablo Isaza Relatório

0

as @Locke said, your for loop syntax is incorrect, as You are missing i in the comparison. Other than that, you code is flooded with typos. You didn't close the for loop, and you didn't need to return ranpassword. Now your code works, notice that the number I put as a parameter in the onclick in the HTML is the length your password will be. Also, instead of using .chatAt, I generate a random number and add it to ranpassword. For example: The for loop gets chara[4], so randpassword has chara's 5th letter.

function password(length) {
  var ranpassword = "";
  var chara = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"


  var charalength = chara.length // there is an outside variable defining charalength = ""; I could not include that here 
  for (var i = 0; i < length; i++) {
    ranpassword += chara[(Math.floor(Math.random() *
      chara.length))];
  }
  document.getElementById("p4").innerHTML = "hello there " + ranpassword;
}
<button id = "button5" onclick = "password(4)">Generate password </button> 
<p4 id = "p4" > Your password will apear here </p4>
 

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda