javascript - 循环时如何继续?
问题描述
用户正在输入他/她的密码,直到密码正确为止。当用户想要单击“取消”时,我们会询问“您确定吗?” 如果答案是否定的,则再次询问密码。
我尝试再次使用Strat,但它不起作用=(
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");
}
}
解决方案
在循环开始时询问密码,在你得到密码之前不要中断:
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");
}
}
推荐阅读
- c# - 在 WPF 中按日期和列表索引自动排序 DataGrid
- python - 计算 3D 阵列的最小方向
- c# - 我在 ASP 格式化时遇到问题
- javascript - 如何在 React 中将数据从 Form 传递到 mapDispatchToProps
- c# - InvalidCastException:无法将“System.Guid”类型的对象转换为“System.String”类型。在 asp.net 核心 webapi 中
- python - matplotlib 中的径向热图
- c - 从没有 FPO 的定点整数重建 IEEE754 浮点二进制
- python - pyTelegramBot 装饰器未检测到更新
- python - 我们如何在使用 python 的滚动平均期间拒绝包含异常值的窗口?
- algorithm - Gnome 排序的优缺点是什么