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:
Here, the correct username is entered in 3 attempts, therefore, the program will ask for the password.
After the correct password script is terminated.