简体   繁体   中英

Javascript: Alert message after loop ends

This is a simple login excercise for school. It is meant to give you 3 attempts to log in. I would like to make it so after the loop stops (the three attempts were used), it alerts the user that he has no remaining attempts and his account will be blocked.

Something like:

alert("You don't have any attempts left. Your account is now blocked);

Here is the loop I made:

var tries;

for (tries = 2; tries !== -1; tries--) {
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
        if (User === "hello" && Pass === "world") {
            alert("Welcome.");
            break;
        } else {
            alert("Incorrect username and/or password. You have " + tries + " attempt(s) left.");
        }
}

Thanks in advance.

You where very close. I think this is what you want.

 var tries; for (tries = 2; tries >= 0; tries--) { let User = prompt("Enter your username:"); let Pass = prompt("Enter your password:"); if (User === "hello" && Pass === "world") { alert("Welcome."); break; } else if (tries == 0) { alert("You don't have any attempts left. Your account is now blocked"); } else { alert("Incorrect username and/or password. You have " + tries + " attempt(s) left."); } } 

You can achieve this recursively. Just decrease the number of Tries everytime wrong username or password is entered.

var TRIES = 3;

function ask() {
  let User = prompt("Enter your username:");
  let Pass = prompt("Enter your password:");

  if (User === "hello" && Pass === "world") {
    return alert("Welcome.");
  }

  if (TRIES > 0) {
    alert("Incorrect username and/or password. You have " + TRIES + " attempt(s) left.");
    TRIES -= 1;
    ask()
  } else {
    alert("You don't have any attempts left. Your account is now blocked");
  }
}

ask()
var tries;

for (tries = 0; tries < 3; tries++) {
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
        if (User === "hello" && Pass === "world") {
            alert("Welcome.");
            break;
        } else {
            alert("Incorrect username and/or password. You have " + tries + " attempt(s) left.");
        }
        if(tries == 2)
        {
            alert("You don't have any attempts left. Your account is now blocked);
        }
}

Perhaps you could achieve this by doing the following:

 for (var attemptsRemaining = 3; attemptsRemaining > 0; attemptsRemaining--) { let User = prompt("Enter your username:"); let Pass = prompt("Enter your password:"); if (User === "hello" && Pass === "world") { alert("Welcome."); break; } else if(attemptsRemaining <= 1) { alert("To many failed attempts. Your account is now blocked."); } else { alert("Incorrect username and/or password. You have " + (attemptsRemaining - 1) + " attempt(s) left."); } } } 

The idea here is to add an additional check to see if the number of attemptsRemaining has reached one (or less, for robustness) at which point all attempts are expired. In this case, you display a popup to alert notifying the user that their account is now blocked.

Hope that helps!

 let tries = 0; for (tries = 3; tries-->0;) { let User = prompt("Enter your username:"); let Pass = prompt("Enter your password:"); if (User === "hello" && Pass === "world") { break; } if (tries>0) { alert("Incorrect username and/or password. You have " + tries + " attempt(s) left."); } } if (tries<0) { alert("You don't have any attempts left. Your account is now blocked"); } else { alert("Welcome."); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM