Restart while while JavaScript

1

I have been testing some things in JavaScript and, since I am learning, in practice I have encountered an error executing a cycle while() that I describe next.

What I need is to make a prompt() that asks for a password to the user to enter it, such as "Miguel", and to return a "welcome" message (Part resolved). Now, I also need to have a limited number of attempts and when I overstep this one I get an "error" message.

I have solved everything with a cycle while() that contains the conditions that I will leave in the example of the code, but the problem is that when executing the condition where it counts the number of attempts ( intento++ ) until fulfilling the maximum of attempts allowed and the code closes the execution ( break; ), therefore does not restart me or continues with the cycle, this means that it does not rerun the prompt() and also reaches the maximum number of attempts without executing all the cycle again.

The idea is to run it in such a way that if I enter an "incorrect" password, add the value of intento once every time the cycle restarts (rerunning from the prompt's appearance until the end), so that if we re-enter the password again this attempt is added again to intento , which does not happen since intento++ is executed until it reaches three and the program has to close to reach the maximum number of attempts, without that the user has entered anything.

var pass = prompt("Introduzca la contraseña");
var intento = 0;

while (pass != undefined) {

  if (intento < 3) {

    if (pass == "Miguel") {

      alert("Bienvenido Miguel.");
      break;

    } else if (pass != "Miguel") {

      intento++;
      alert("Introduzca una clave valida.");
      continue;

    }

  } else {

    alert("Ha intentado demasiadas veces.");
    break;

  }

}
    
asked by Dєηyη Crawford 08.08.2018 в 06:10
source

5 answers

6

The problem is basically here: alert("Introduzca una clave valida."); . If instead of the alert you use something like pass = prompt("Introduzca una clave valida."); it would work, because you would be showing the message and collecting the possible prompt in the variable pass .

However, I propose this logic in which the attempts are discounted and the user is informed that he has n attempts. This is how these kinds of things usually work.

Note that pendingIntents is set to 2 because the user will have exhausted the first possibility in the 1st prompt, and therefore only have two left.

var pendingIntents=2;
var pass = prompt('Introduzca la contraseña. Dispone de ${pendingIntents+1} intentos');
while (pass != undefined) {

  if (pendingIntents > 0) {

    if (pass == "Miguel") {

      alert("Bienvenido Miguel.");
      break;

    } else if (pass != "Miguel") {

      pass = prompt('Introduzca una clave valida. Quedan ${pendingIntents} intentos');
      pendingIntents--;
      continue;

    }

  } else {

    alert("Ha intentado demasiadas veces.");
    break;

  }

}
    
answered by 08.08.2018 / 08:32
source
1

There are a few things that catch my attention in your code:

  • The entire loop starts only if the entered password is not undefined, but nothing happens at all if it is, that is, samples that a password is incorrect, but an indefinite one would not need to be noted that is it?

  • The continue is used to jump one of the iterations of a loop, but in your case the fact that this is equal to not this, does not make any change because if you let the loop terminates naturally will not execute any code lien nor enter any if :

Explanation:
The problem of the end of blow comes to that there is nothing to change the pass at the time you do continue . You only make an alert but there is no other time in which you can set a new password, therefore the loop is executed again with the exact same error errone before the number of attempts is skipped.

Solution:
In this way you can manage in a better way the errors that the code can generate:

pass = prompt("Introduzca la contraseña");
var intentosFallidos = 1;
// Si entra en el else significa que ya ha realizado un intontoFallido.
var correcto = false;

// Repetir el bucle mientras los intentos fallidos sean menores a 3
// Y mientras que no haya acertado la contraseña
 
while(intentosFallidos<3 && !correcto){
  // Si la contraseña coincide: El login es correcto
  if(pass == "Miguel"){
    correcto = true;
  // sino el numero de intentos fallidos se aumenta y se vuleve a preguntar por otra
  } else {
    intentosFallidos++;
    pass = prompt("Introduzca una clave valida");
  }
}
// A este punto solo se llega si bien se ha realizado correctamente el login
// o bien se ha superado el numero de intentos, por lo tanto:
if(correcto){
  alert("Bienvenido Miguel")
}else {
  alert("Ha superado el maxion de intentos");
}
    
answered by 08.08.2018 в 09:12
1

Do not really mess up, try this easy code and it works:

'use strict'

var contra;
var contador=0;

while(contador<=2 && contra!='Miguel'){
    contra = prompt('Introduce contra');

    if(contra==''){
        console.log('No has puesto nada');
    }

    contador++;
    alert(contador);
}

if(contra=='Miguel'){
    console.log('Bienvenido Miguel');

}else{
    console.log('Se acabaron los intentos');
}
    
answered by 08.08.2018 в 17:00
0

Try this way:

do{
    var pass = prompt("Introduzca la contraseña");
    var intento = 0;
    var intentoAcertado = false;

    while (pass != undefined) {

      if (intento < 3) {

        if (pass == "Miguel") {

           alert("Bienvenido Miguel.");
           intentoAcertado=true; 
           break;

        } else if (pass != "Miguel") {

           intento++;
           alert("Introduzca una clave valida.");
           continue;

        }

     } else {

        alert("Ha intentado demasiadas veces.");
        break;

     }

    }
}while(!intentoAcertado)
    
answered by 08.08.2018 в 06:27
0

In your code, there is no progressive counter nor a variable that decreases its amount as the user enters a data that does not correspond to the correct answer.

One possibility is to do a while with a counter inside it or make a for loop.

    
answered by 08.08.2018 в 08:56