Javascript:循环结束后的警报消息

Javascript: Alert message after loop ends

这是一个简单的学校登录练习。它的目的是给你3次登录尝试。我希望在循环停止后(使用了三次尝试),它提醒用户没有剩余的尝试,他的帐户将被阻止。

类似:

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

这是我做的循环:

1
2
3
4
5
6
7
8
9
10
11
12
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.");
        }
}

事先谢谢。


你离得很近。我想这就是你想要的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.");
        }
}


您可以递归地实现这一点。只要减少每次输入错误用户名或密码时的尝试次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.");
}


也许您可以通过执行以下操作来实现这一点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.");
    }
  }
}

这里的想法是增加一个额外的检查,看看attemptsRemaining的数量是否已经达到一个(或更少,为了健壮性),此时所有的尝试都已过期。在这种情况下,您将显示一个弹出窗口,通知用户其帐户现在被阻止。

希望有帮助!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
        }
}