I've just started learning JS and I got stuck here. What I need to do:
- If you don't enter the correct password, you get the message "Try again".
- If you don't enter the password 3 times, you get the message "No more tries left".
- If you enter the correct password, you get the message "You may enter".
Here's my code:
for ( let i = 3; i > 0; i-- ) {
let password = prompt("What is the password?")
if ( password.toUpperCase() !== "BINGO" ) {
alert("Try again")
} else if ( i = 0 ) {
alert("No more tries left")
} else {
alert("You may enter")
}
}
I can't get it work properly as the message "No more tries left" doesn't show up. I know ( i = 0 )
is wrong but I don't know how to make it work.
CodePudding user response:
Firstly, make sure that you're not using an assignment operator when you check if i == 0
. You're currently using i = 0
, which doesn't check if the two are equal as much as it assigns the left to the right. No bueno.
Secondly, your for loop is off just by a bit. It'll never get to 0 because you've asked it to loop while i > 0
, not i >= 0
. But wait - if you use i >= 0
, it'll loop four times. That's not what you want either. We'll compromise and loop three times, but check if i == 1
instead of 0.
Here's my corrected code that works:
// loop three times
for ( let i = 3; i > 0; i-- ) {
let password = prompt("What is the password?")
// if it's the correct answer then alert and break the loop
if ( password.toUpperCase() == "BINGO" ) {
alert("You may enter")
break
// if it's not, and the tries has elapsed, then alert and break the loop
} else if ( i == 1 ) {
alert("No more tries left")
break
// if it's not but the tries have not elapsed, then loop again
} else {
alert("Try again")
}
}
CodePudding user response:
Try this.
for ( let i = 3; i >= 0; i-- ) {
if (i===0){
alert("No more tries left");
break;
}
let password = prompt("What is the password?")
if ( password.toUpperCase() !== "BINGO" ) {
alert("Try again")
} else {
alert("You may enter")
}
}
Give the user chance to enter the password 3 times but loop 4 times and check if the loop runs for 4th times(i === 0). if prompt No more tries left
and break the loop.
CodePudding user response:
You are using an assignment operator instead of a comparison operator. Change this:
else if ( i = 0 ) {
alert("No more tries left")
}
To this :
else if ( i == 0 ) {
alert("No more tries left")
}
CodePudding user response:
Try this. It will run the loop until they have entered the password correctly or the number of attempts is 3.
After the loop you can then just check if valid is true or false.
let valid = false;
let attempts = 0;
do
{
const password = prompt('What is the password');
valid = password.toUpperCase() === 'BINGO';
if (!valid && attempts < 2)
{
alert('Try again');
}
attempts ;
} while (!valid && attempts < 3)
if (valid)
{
alert('You may enter');
} else
{
alert('No more tries left');
}
CodePudding user response:
You are asking the user to enter the password in each iteration using a loop. So, showing that "you don't have anymore attempt left" is useless here. Because if the value is greater than 3, the instruction inside the loop will not be executed.
Do not use JavaScript for authentication. It is a client side programming language and any one who knows how things work can extract the password. Instead use a back-end language for authentication such as PHP, Node.js
But if you only want to know about it just for the learning purpose not because you wanna implement it, the below code will help you
for (let i=0; i<3; i ){
let password = prompt("what is the password: ");
if (password.toUpperCase() != "BINGO"){
alert("Try Again!");
}else{
alert("You may enter!");
i=3;
}
}
- There are several ways you can do the "No more attempt left" is, one of the simple and basic is:
<input type="button" value="Enter Code" onclick="checkMe()">
<script>
let i=0;
function checkMe(){
if (i<3){
let password = prompt("Enter password!");
if (password.toUpperCase() != "BINGO"){
alert("Try Again! Attempt " ( i));
}else{
alert("You may enter!");
i=3;
}
}else alert("No more attempts left");
}
</script>
- The above code can be implemented using input field, as i said, there are several ways. Hope it helps!
CodePudding user response:
Your code is fine, what goes wrong is that it does not fall into the if condition (i == 0)
, because the loop only runs while i > 0
,
just need to adjust like this:
for ( let i = 3; i > 0; i-- ) {
let password = prompt("What is the password?")
if (password.toUpperCase() !== "BINGO" && i > 1 ) {
alert("Try again")
} else if (i == 1 && password.toUpperCase() !== "BINGO") {
alert("No more tries left")
} else {
alert("You may enter")
i = 0
}
}