How to make the counter work to allow three attempts?

Asked

Viewed 156 times

0

Good morning guys All right? I am taking a beginner course in JS and I am on an issue that I can not solve at all. The question is this: "Change the previous code so that the user has 3 attempts to log in." The code that came from Alura is as follows:

`

var loginCadastrado = "alura";
var senhaCadastrada = "alura321";

var loginInformado = prompt("Informe seu login");
var senhaInformada = prompt("Informe sua senha");

if( loginCadastrado == loginInformado && senhaCadastrada == senhaInformada ) {

    alert("Bem-vindo ao sistema " + loginInformado);
} else {

    alert("Login inválido. Tente novamente");
}

`

The code I made is this : `

var login_cadastrado = "alura";
var senha_cadastrada = "alura123";

var login_informado = prompt("Informe o login cadastrado");
var senha_informada = prompt("Informe sua senha");

var contador = 1;

while(contador <= 3){
if(login_cadastrado == login_informado && senha_cadastrada == senha_informada){
    alert("Bem vindo ao sistema " + login_informado);
    break;
}
contador = contador + 1;
else{
    alert("usuário ou senha incorreto");


}


</script>`

I made another version with the counter below Else but this one goes into infinite looping, does not increase the counter and I do not know how to solve. Can you help me? I’m about 4 days trying to solve and I can’t, I don’t want to look at the teacher’s code before I actually learn how to make the code.

Thanks in advance

  • Put at least your HTML tb in the question, it will help someone to answer you

3 answers

0

To ask login 3x you also need to put the "prompt" within the loop of repetition.

To increment the counter you can directly use the operator "++". This operator, when used before the variable it first increments and then compares the condition. When used later it does the opposite, first compares the condition and then increments.

In this example using the "while" first run the code and then do the condition comparison to perform the loop.

var login_cadastrado = "alura";
var senha_cadastrada = "alura123";
var contador = 0;

do {
    var login_informado = prompt("Informe o login cadastrado");
    var senha_informada = prompt("Informe sua senha");

    if (login_cadastrado == login_informado && senha_cadastrada == senha_informada){
        alert("Bem vindo ao sistema " + login_informado);
        break;
    } else {
        alert("usuário ou senha incorreto");
    }
} while (++contador < 3);

I recommend you practice logic by changing this script and also trying to use other types of loops. Also, good studies.

0

You are adding more numbers to your counter, that is, every time your counter receives another number, because of the "+" sign in that part:

contador = contador + 1;

This is creating an infinite loop. Try to do something on this model:

var login_cadastrado = "alura";
var senha_cadastrada = "alura123";

var login_informado = prompt("Informe o login cadastrado");
var senha_informada = prompt("Informe sua senha");

var contador = 3;

while(contador <= 3){
    contador = contador - 1;
    if(contador = 0){
        alert("Desculpe, você atingiu o limite de tentativas.")
    }else{
        if(login_cadastrado == login_informado && senha_cadastrada == senha_informada){
            alert("Bem vindo ao sistema " + login_informado);
        break;
        }
        else{
            alert("Usuário ou senha incorreto. Você tem mais " + contador + "tentativas.");
        }
    } 
}

In case it doesn’t work, let me know, I had no way to test it because I didn’t have the rest of the code.

0

var login_cadastrado = "alura";
var senha_cadastrada = "alura123";

function login(n) {
  for (var tentativa = 0; tentativa < n; ++tentativa) {
    var login_informado = prompt("Informe o login cadastrado");
    var senha_informada = prompt("Informe sua senha");

    if(login_cadastrado == login_informado && senha_cadastrada == senha_informada) {
      alert("Bem vindo ao sistema " + login_informado);
      return true;
    } else {
      alert("usuário ou senha incorreto");
    }
  }
  
  return false;
}

login(3);

Browser other questions tagged

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