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

112
Visualizações
i am trying to create a simple login page using javascript that will allow the user to input their username and password correctly until 3 tries ONLY

i am new to javascript and i was not able to figure out how to do it correctly. i was trying to create a simple page that will allow the user to input their username and password correctly but if they enter an invalid username or password 3 times, they won't be allowed to input again. here is my code:

var UserInfo=[
{
    username:"elliot",
    password:"123"
},
{
    username:"nicholas",
    password:"456"
},
{
    username:"rose",
    password:"789"
},
{
    username:"samantha",
    password:"987"
},
{
    username:"austin",
    password:"654"
}
]

document.getElementById("btn").onclick=function(){
var username=document.getElementById("un").value
var password=document.getElementById("pw").value

    for(i=0;i<4;i++){
        if (username==UserInfo[i].username && password==UserInfo[i].password){
            alert("Successfully logged in!");
        }else{
            alert("Please try again.");
        }
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You cannot use for loop for that. You need a counter to count how many attempts and increase that counter every time the user makes a mistake. Try something like this:

let attempts = 0
document.getElementById("btn").onclick=function(){
var username=document.getElementById("un").value
var password=document.getElementById("pw").value

    if(attempts>=3){
        alert("Sorry. Too many attempts.");
    }
    else{
        if (UserInfo.find(el=> el.username === username && el.password === password)){
            alert("Successfully logged in!");
        }else{
            attempts++
            alert("Please try again.");
        }
    }
    
}
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