0
1.1kviews
JavaScript Program for guessing the username and password in 5 attempts.

Write a JavaScript program that prompts for the username and password individually.

Provides five chances to guess the correct username and password for each one.

The correct username is "admin" and the password is "admin12345".

If the username and password are correct it will display on the screen how many tries the user made to guess the correct password otherwise it will display how many tries the user has left to guess the correct password.

If the correct username is entered in 5 attempts then only the program will ask for the password otherwise not.

In 5 attempts correct username is not guessed then the program will terminate there only.

1 Answer
0
23views

The Javascript Program

  • The below javascript program first ask username from the user.

  • The program gives 5 chances to the user to guess the correct username.

  • If the user gives the wrong username then the program prompts the user they entered the wrong username and the chances remain to guess the correct username.

  • If the user gives the correct username then the program prompts the user they entered the correct username and in how many attempts the user guessed the correct username.

  • If the user guesses the correct username then only the program will ask for the password otherwise program will terminate there only without asking for the password.

  • Similar, to the username program give 5 chances for the user to guess the correct password.

  • If the user gives the wrong password then the program prompts the user they entered the wrong password and the chances remain to guess the correct password.

  • If the user gives the correct password then the program prompts the user they entered the correct password and in how many attempts the user guessed the correct password.

  • Finally, after receiving the correct password program will terminate.

<script>
var i = 5;
var j = 5;
while(i != 0)  // Loop runs for 5 times
{ 
var userName = "admin";  // Username is set as "admin"
var username = prompt("Enter Username"); // Asking for Username from the user
if(username == userName)   
// Checking the condition whether entered username is matched with set username or not
{
i = 6 - i;
window.alert("Your Username is Correct \n In "+i+" attempts, your guess is correct now !!!!!"); 
// Display message if correct username is entered and after how many tries user made the correct guess
while(j != 0)  // Loop runs for 5 times
{
var userPassword = "admin12345"; // Password is set as "admin12345"
var password = prompt("Enter your password") // Asking for Password from the user
if(password == userPassword)  
// Checking the condition whether entered password is matched with set password or not
{
j = 6 - j;
window.alert("Your Password is Correct \n In "+j+" attempts, your guess is correct now !!!!!"); 
// Display message if correct password is entered and after how many tries user made the correct guess 
break;
}
else{
j = j - 1;
window.alert("your Password is Not Correct \n Now you have only " + j + " tries left"); 
//  Display message if password is wrong and number of tries left for the user
}
}
break;
}
else{
i = i - 1;
window.alert("Your Username is Not Correct \n Now you have only " + i + " tries left"); 
// Display message if username is wrong and number of tries left for the user
}
}
</script>

The Output of the above Program:

Username

Here, the correct username is entered in 3 attempts, therefore, the program will ask for the password.

Password

After the correct password script is terminated.

Please log in to add an answer.