Home > Mobile >  How to return to the same condition if the password is wrong twice
How to return to the same condition if the password is wrong twice

Time:12-12

When you input wrong password, incorrect alert show, but second time when you do it, it just let you on site.

var password = "Password123";
          var pass = window.prompt("Enter password: ");
          if (pass == sifra) {
            alert("Correct");
          } else {
            var pass = window.prompt("Incorrect password: ");
          }

CodePudding user response:

You cause your code has no checks after the second prompt. In this scenario, You should write your code in a kind of recursion way. When the password check fails, It should begin the same process again and again until it passes.

// fn: verify the password
function verifyPassword(pass) {
  if (pass === "sifra") {
    alert("Correct");
  } else {
    // prompt the password again if it's incorrect
    promptPassword("Incorrect password: ");
  }
}

// fn: promoting the password
function promptPassword(msg = "Enter password:") {
  var pass = window.prompt(`${msg} `);

  // verify the password
  verifyPassword(pass);
}

promptPassword();

CodePudding user response:

This is happening because second time its just taking input but not validating password if you want to validate password untill its correct you can try this just add a loop

var pass = window.prompt("Enter password: ");
while (pass !== sifra) {
  pass = window.prompt("incorrenct password try again: ");
}
// reaches here only when password is corrent
  • Related