0
1.2kviews

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.

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.

0
24views

## The Javascript Program

• 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
{
// Checking the condition whether entered username is matched with set username or not
{
i = 6 - i;
// 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
{
// Checking the condition whether entered password is matched with set password or not
{
j = 6 - j;
// 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.