0

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.

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
TBG
  • 305
  • 4
  • 10
  • What happens with this code? It loops forever? You have to let us know what the problem is, what you have tried and where the issue is. Hint: `!==` is not the same as `!=`. See https://stackoverflow.com/questions/1946063/in-javascript-is-same-as – Nic3500 Nov 27 '18 at 00:02

4 Answers4

2

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.");
        }
}
MomasVII
  • 4,641
  • 5
  • 35
  • 52
1

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()
Akinjide
  • 2,723
  • 22
  • 28
0
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);
        }
}
0

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!

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65