如果密码和/或用户名错误,我该怎么做才会再次要求输入密码和/或用户名?
var user = prompt("Please Enter Your Username To Continue.").toUpperCase();
if (user === "RYEBREAD4") {
var pass = prompt("Please Enter Your Password To Continue").toUpperCase();
if (pass === "7277") {
console.log("Welcome Back Ryebread4");
} else {
console.log("Username Or Password Is Incorrect! Please Try Again");
}
}
答案 0 :(得分:1)
使用while
循环
var signedIn = false;
while(!signedIn)
{
...
if (password === "7477")
{
console.log("Welcome back!");
signedIn = true;
}
...
}
或for
(不推荐)
var username = "";
var password = "";
for(;username !== "Ryebread4" && password !== "7477";)
{
//prompt user for username/password
...
}
同样的方法可以应用于while
循环,并消除变量。