Ryebread4
Ryebread4

Reputation: 11

Request for the username and password again if either is wrong

How do i make it so when the password and/or username is wrong it will ask for the password and/or username again?

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");
    }
}

Upvotes: 0

Views: 84

Answers (1)

Liam McInroy
Liam McInroy

Reputation: 4366

Use a while loop

var signedIn = false;
while(!signedIn)
{
...
    if (password === "7477")
    {
        console.log("Welcome back!");
        signedIn = true;
     }
...
}

Or for (not recommend)

 var username = "";
 var password = "";

for(;username !== "Ryebread4" && password !== "7477";)
{
     //prompt user for username/password
      ...
}

That same methodology can be applied to the while loop, and eliminate a variable.

Upvotes: 1

Related Questions