我如何在循环中继续?

问题描述 投票:0回答:1

用户正在输入他/她的密码,直到正确为止。当用户想要单击“取消”时,我们询问“您确定吗?”如果答案为否,则再次要求输入密码。

我曾尝试再次进行分层,但不起作用=(


var password = "username",
    user_password        ,
    checked = true       ,
    user_password = prompt("Enter your password"),
    checked_confirm      ;

label: while(checked){
    if(user_password == password){
        alert("You are successfully logged in");
        break;
    }
    else if(user_password == null){
        checked_confirm =  confirm("Are you sure you want to cancel authorization?");
        if(checked_confirm){
            alert("You have canceled authorization");
            break;
        }
        else{
            continue lable;
        }
    }
    else{
        user_password = prompt("Enter your password");
    }
}

javascript continue
1个回答
0
投票

在循环开始时询问密码,直到您得到它之前不要打破`:

var password = "username",
  user_password,
  checked_confirm;

while (true) {
  user_password = prompt("Enter your password");

  if (user_password == password) {
    alert("You are successfully logged in");
    break;
  } else if (user_password == null) {
    checked_confirm = confirm("Are you sure you want to cancel authorization?");
    if (checked_confirm) {
      alert("You have canceled authorization");
      break;
    }
  } else {
    alert("Wrong password");
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.