0

I'm trying to make "Enter your Password: " prompt appear until the correct password is entered. There is no limit to the attempts of password entry. So far I came up with this code using Javascript

    function promptPassword() {

      var name = prompt("Enter your Username: ");
      var pwd = prompt("Enter your Password: ");

      if (pwd == 'P@ssw0rd') {
        alert("Password is correct, you are allowed to enter the site");
      }

      while (pwd != 'P@ssw0rd') {
        alert("Login is incorrect");
        prompt("Enter your Password: ");
      }

    }
<body onload="promptPassword();">
  </body>

When I enter the correct password in first attempt the correct alert displays and it goes away when I click 'ok', which is expected. When I type wrong passwords it keeps prompting to enter the password again, which is also as expected. But the problem here is when I first enter wrong password and then enter right password in the next attempts, it still keeps prompting to enter the password again. Once the correct password is entered, "Password is correct, you are allowed to enter the site" alert should display and should go away, when 'ok' is clicked

2
  • 2
    There is the assignment to 'pwd' missing in your second prompt() call Commented Nov 22, 2016 at 18:44
  • 1
    You are not re-assigning the value to pwd Commented Nov 22, 2016 at 18:47

3 Answers 3

4

Your prompt return value isn't being assigned in the while loop. Try this:

function promptPassword( )
{

var name = prompt ("Enter your Username: ");
var pwd = prompt ("Enter your Password: ");

while (pwd != 'P@ssw0rd'){
alert("Login is incorrect");
pwd = prompt ("Enter your Password: ");
}

alert("Password is correct, you are allowed to enter the site");

}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for helping me. I tried this the prompt does not appear anymore. But still I need to see the alert "Password is correct, you are allowed to enter the site". But when I enter the correct password in the second attempt I don't see that alert
You'd need to put the alert("Password is correct") after the while loop. I've updated my answer.
You need to run function after init it. like promptPassword(). +1 for answer but anyone can see this script by see source code.
2

You need to set pwd to prompt("Enter your password: ");. If you don't, it will keep asking you, but the password variable will never update, and you'll be stuck in the loop forever!

function promptPassword( )
{
    
    var name = prompt ("Enter your Username: ");
    var pwd = prompt ("Enter your Password: ");
    
    while (pwd != 'P@ssw0rd'){
        alert("Login is incorrect");
        pwd = prompt ("Enter your Password: ");
    }
    
    alert("Password is correct, you are allowed to enter the site");
    
}
promptPassword();

3 Comments

Thanks for helping me. I tried this the prompt does not appear anymore. But still I need to see the alert "Password is correct, you are allowed to enter the site". But when I enter the correct password in the second attempt I don't see that alert
Ah, well then you need to add that too. Fixin' it!
Thanks a lot buddy
-1
enter code here function promptPassword( )

{

var name = prompt ("Enter your Username: ");
var pwd = prompt ("Enter your Password: ");

while (pwd != 'P@ssw0rd'){
    alert("Login is incorrect");
    pwd = prompt ("Enter your Password: ");
}

alert("Password is correct, you are allowed to enter the site");
while ( name != "Youssef" ) {      alert("Login is incorrect");
    name = prompt ("Enter your Username: ");
} } promptPassword();

2 Comments

Please copy and paste onto your console (ctrl + shift + j) (copy all code)
New code :function promptPassword( ) { var name = prompt ("Enter your Username: "); var pwd = prompt ("Enter your Password: "); while (pwd != 'P@ssw0rd'){ alert("Login is incorrect"); pwd = prompt ("Enter your Password: "); } alert("Password is correct, you are allowed to enter the site"); while ( name != "Youssef" ) { alert("Login is incorrect"); name = prompt ("Enter your Username: "); } } promptPassword();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.